设置tf.变量Python/TensorFlow的初始值
我有这个功能:设置tf.变量Python/TensorFlow的初始值,python,python-2.7,tensorflow,Python,Python 2.7,Tensorflow,我有这个功能: def new_weights(shape): return tf.Variable(tf.truncated_normal(shape, stddev=0.05)) 我这样称呼它,例如: # shape = [filter_size, filter_size, num_filters, num_input_channels] shape = [1, 1, 8, 1] weights = new_weights(shape) 我想用以下值初始化权重: weights
def new_weights(shape):
return tf.Variable(tf.truncated_normal(shape, stddev=0.05))
我这样称呼它,例如:
# shape = [filter_size, filter_size, num_filters, num_input_channels]
shape = [1, 1, 8, 1]
weights = new_weights(shape)
我想用以下值初始化权重:
weights = [1, 2, 3, 4, 5, 6, 7, 8]
在用这些值初始化它之后,我希望它被更新(可训练)
如何执行此操作?您可以使用分配功能
shape = [1, 1, 8, 1]
weights = new_weights(shape)
ws = [1, 2, 3, 4, 5, 6, 7, 8]
ws = np.array(ws).reshape(shape)
weights = weights.assign(ws)
我认为您可以使用如下函数:
def new_weights(shape):
total = np.prod(shape)
init_data = np.array(range(1, 1+ total)).reshape(shape)
return tf.get_variable(name='weights',
initializer = tf.constant_initializer(init_data),
shape = shape)
并检查它:
shape = [1, 1, 8, 1]
weights = new_weights(shape)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(weights))
关于tf.Variable(初始值=权重,…)?这并不是问题的答案。若要评论或要求作者澄清,请在其帖子下方留下评论-