Tensorboard Python应用程序中的投影仪

时间:2018-11-22 07:41:25

标签: python python-3.x tensorflow visualization tensorboard

我有以下代码和示例可以正常工作,并且我确实希望这样做:

import numpy as np
import pandas as pd
import sklearn
import sklearn.preprocessing
import datetime
import os
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.contrib.tensorboard.plugins import projector

valid_set_size_percentage = 3
test_set_size_percentage = 3
seq_len = 5 # choose sequence length
df = pd.read_csv("Test.csv", encoding = 'utf-16',sep=',',index_col = 0)
df.head()
def normalize_data(df):
    cols = list(df_stock.columns.values)
    min_max_scaler = sklearn.preprocessing.MinMaxScaler()
    df = pd.DataFrame(min_max_scaler.fit_transform(df.values))
    df.columns = cols
    return df
def load_data(stock, seq_len):
    data_raw = stock.as_matrix() # convert to numpy array
    data = []
    print(data_raw.shape)
    for index in range(len(data_raw) - seq_len): 
        data.append(data_raw[index: index + seq_len])
    data = np.array(data);
    valid_set_size = int(np.round(valid_set_size_percentage/100*data.shape[0]));  
    test_set_size = int(np.round(test_set_size_percentage/100*data.shape[0]));
    train_set_size = data.shape[0] - (valid_set_size + test_set_size);
    x_train = data[:train_set_size,:-1,:]
    y_train = data[:train_set_size,-1,:4]
    x_valid = data[train_set_size:train_set_size+valid_set_size,:-1,:]
    y_valid = data[train_set_size:train_set_size+valid_set_size,-1,:4]
    x_test = data[train_set_size+valid_set_size:,:-1,:]
    y_test = data[train_set_size+valid_set_size:,-1,:4]
    return [x_train, y_train, x_valid, y_valid, x_test, y_test]
df_stock = df.copy()
cols = list(df_stock.columns.values)
print('df_stock.columns.values = ', cols)
df_stock_norm = df_stock.copy()
df_stock_norm = normalize_data(df_stock_norm)
x_train, y_train, x_valid, y_valid, x_test, y_test = load_data(df_stock_norm, seq_len)
print(y_train[:2])
print('x_train.shape = ',x_train.shape)
print('y_train.shape = ', y_train.shape)
print('Inputs = ',x_train.shape[2])
print('Outputs = ', y_train.shape[1])
print('x_valid.shape = ',x_valid.shape)
print('y_valid.shape = ', y_valid.shape)
print('x_test.shape = ', x_test.shape)
print('y_test.shape = ',y_test.shape)
index_in_epoch = 0;
perm_array  = np.arange(x_train.shape[0])
np.random.shuffle(perm_array)
def get_next_batch(batch_size):
    global index_in_epoch, x_train, perm_array  
    if index_in_epoch > x_train.shape[0]:
        start = 0 # start next epoch
        index_in_epoch = 0#batch_size
    start = index_in_epoch
    index_in_epoch += batch_size       
    end = index_in_epoch
    return x_train[perm_array[start:end]], y_train[perm_array[start:end]]
n_steps = seq_len -1
n_inputs = x_train.shape[2]
n_neurons = 100
n_outputs = y_train.shape[-1]
n_layers = 2
learning_rate = 0.001
batch_size =10
n_epochs = 100
train_set_size = x_train.shape[0]
test_set_size = x_test.shape[0]

tf.reset_default_graph()

X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
y = tf.placeholder(tf.float32, [None,n_outputs])


layers = [tf.contrib.rnn.LSTMCell(num_units=n_neurons, 
                                 activation=tf.nn.leaky_relu, use_peepholes = True)
         for layer in range(n_layers)]
multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers)
rnn_outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32)
stacked_rnn_outputs = tf.reshape(rnn_outputs, [-1, n_neurons]) 
stacked_outputs = tf.layers.dense(stacked_rnn_outputs, n_outputs)

outputs = tf.reshape(stacked_outputs, [-1, n_steps, n_outputs])

outputs = outputs[:,n_steps-1,:] # keep only last output of sequence

loss = tf.reduce_mean(tf.squared_difference(outputs, y)) # loss function = mean squared error 

optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) 
training_op = optimizer.minimize(loss)
saver = tf.train.Saver()

with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer())
    for iteration in range(int(n_epochs*train_set_size/batch_size)):
        x_batch, y_batch = get_next_batch(batch_size) # fetch the next training batch 

        sess.run(training_op, feed_dict={X: x_batch, y: y_batch}) 

        if iteration % int(1*train_set_size/batch_size) == 0:

            mse_train = loss.eval(feed_dict={X: x_train, y: y_train}) 
            mse_valid = loss.eval(feed_dict={X: x_valid, y: y_valid}) 
            mse_test = loss.eval(feed_dict={X: x_test, y: y_test})

            print('%.2f epochs: MSE train/valid/test = %.3f/%.3f/%.3f'%(
                iteration*batch_size/train_set_size, mse_train, mse_valid,mse_test))
            try:
                save_path = saver.save(sess, "modelfile\\model"+str(iteration)+".ckpt")
            except Exception as e:
                print(e)
                if not os.path.exists("modelfile\\"):
                    os.makedirs("modelfile\\")

                save_path = saver.save(sess, "modelfile\\model"+str(iteration)+".ckpt")

以下是我要执行的示例:
Same data Please click and see

我愿意在代码中添加Tensorboard的投影仪。但是我不明白如何做到这一点。我想可视化我为培训提供的各种输入。我提供以下各列,并尝试预测ohlc的值。

'o', 'h', 'l', 'c', 'rel1', 'rel2', 'rel3', 'rel4', 'rel5', 'rel6', 'rel7', 'rel8'  

我想在投影机中可视化以上各列,以了解它们之间的相互关系,以便为我提供输出。
请让我知道我能做什么才能得到我愿意的。

已编辑

我尝试了以下操作,但看不到投影仪选项卡:

tf.reset_default_graph()

X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
y = tf.placeholder(tf.float32, [None,n_outputs])

symbols = tf.placeholder(tf.int32, [None, 1], name='stock_labels')
embed_matrix = tf.Variable(
                tf.random_uniform([1, n_inputs],0.0, 1.0),
                name="embed_matrix"
            )
stacked_symbols = tf.tile(symbols, [batch_size,n_steps], name='stacked_stock_labels')
stacked_embeds = tf.nn.embedding_lookup(embed_matrix, stacked_symbols)
# stacked_embeds = tf.nn.embedding_lookup(embed_matrix)

# After concat, inputs.shape = (batch_size, num_steps, input_size + embed_size)
inputs_with_embed = tf.concat([X, stacked_embeds], axis=2, name="inputs_with_embed")
embed_matrix_summ = tf.summary.histogram("embed_matrix", embed_matrix)

并在会话代码中编辑了以下几行:

merged_sum = tf.summary.merge_all()
    global_step = 0
        # Set up the logs folder
    writer = tf.summary.FileWriter('logs')
    writer.add_graph(sess.graph)
    projector_config = projector.ProjectorConfig()

            # You can add multiple embeddings. Here we add only one.
    added_embed = projector_config.embeddings.add()
    added_embed.tensor_name = embed_matrix.name
    # Link this tensor to its metadata file (e.g. labels).
    shutil.copyfile("logs\\metadata.tsv",
                    "logs\\metadata1.tsv")
    added_embed.metadata_path = "metadata.tsv"

    # The next line writes a projector_config.pbtxt in the LOG_DIR. TensorBoard will
    # read this file during startup.
    projector.visualize_embeddings(writer, projector_config)
    sess.run(tf.global_variables_initializer())
    if iteration % int(1*train_set_size/batch_size) == 0:
        global_step += 1
        mse_train = loss.eval(feed_dict={X: x_train, y: y_train}) 
        mse_valid = loss.eval(feed_dict={X: x_valid, y: y_valid}) 
        mse_test = loss.eval(feed_dict={X: x_test, y: y_test})
        _,train_merge = sess.run([outputs,merged_sum], feed_dict={X: x_train, y: y_train})
        writer.add_summary(train_merge, global_step=global_step)

这里是metadata.tsv file
请让我知道我错过了什么。

0 个答案:

没有答案