如何在tensorflow中使用没有形状的变量

时间:2018-04-18 13:47:36

标签: tensorflow

我尝试在tensorflow中使用没有形状的变量。我的代码是

sen_var_1 = tf.Variable(np.float32, trainable=False, validate_shape=False, expected_shape=[None, None, 300])
sen_1 = tf.placeholder(shape=[None, None, 300], dtype=np.float32, name="q1")
sen_assign_1 = tf.assign(sen_var_1, sen_1, validate_shape=False)

我将在列车开始时与sen_assign_1进行会话,并且每个时代我都想使用sen_var_1。但似乎expected_shape在sen_var_1中无效。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

Are you making sure you feed something in for the placeholder "sen_1"? Here's a self contained example that runs

import tensorflow as tf
import numpy as np
sen_var_1 = tf.Variable(np.float32, trainable=False, validate_shape=False, expected_shape=[None, None, 300])
sen_1 = tf.placeholder(shape=[None, None, 300], dtype=np.float32, name="q1")
sen_assign_1 = tf.assign(sen_var_1, sen_1, validate_shape=False)

with tf.Session() as sess:
    print(sess.run(sen_1,feed_dict={sen_1:np.zeros((2,2,300))}))