tensorflow在尝试使用python在Tensorboard上可视化权重时给出错误

时间:2018-09-03 10:53:19

标签: python python-3.x tensorflow

这是我尝试过的:

index_in_epoch = 0;
perm_array  = np.arange(x_train.shape[0])
np.random.shuffle(perm_array)

# function to get the next batch
def get_next_batch(batch_size):
    global index_in_epoch, x_train, perm_array  
    if index_in_epoch > x_train.shape[0]:
        #np.random.shuffle(perm_array) # shuffle permutation array
        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]]

# parameters
n_steps = seq_len -1
n_inputs = x_train.shape[2]#4
n_neurons = 100
n_outputs = y_train.shape[-1]#4
n_layers = 2
learning_rate = 0.001
batch_size =10
n_epochs = 1000#200 
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)]

with tf.name_scope("weights"):
    weights = tf.Variable(tf.truncated_normal([n_steps, n_inputs]))
with tf.name_scope("bias"):
    biases = tf.Variable(tf.truncated_normal([n_outputs]))                                                                     
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()

x_train.shape = (55413, 4, 1)y_train.shape = (55413, 1)

with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer())

    summary_writer = tf.summary.FileWriter('logs', sess.graph)
    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})
            y_train_pred,accuracy_output_train,mer = sess.run([outputs,accuracy,merged], feed_dict={X: x_train,y:y_train})
            print('%.2f epochs: MSE train/valid/test = %.10f/%.10f/%.10f'%(
                iteration*batch_size/train_set_size, mse_train, mse_valid,mse_test))
            save_path = saver.save(sess, "modelsOHLC\\model"+str(iteration)+".ckpt")

我收到以下错误:

  

ValueError:提取参数不能解释为张量。 (Tensor Tensor(“ Cast:0”,shape =(?,),dtype = float32)不是该图的元素。)

我错过了什么?我需要改进什么?

0 个答案:

没有答案