Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
设置tf.变量Python/TensorFlow的初始值_Python_Python 2.7_Tensorflow - Fatal编程技术网

设置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(初始值=权重,…)?

这并不是问题的答案。若要评论或要求作者澄清,请在其帖子下方留下评论-