Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.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
Python 重用Tensorflow变量_Python_Tensorflow - Fatal编程技术网

Python 重用Tensorflow变量

Python 重用Tensorflow变量,python,tensorflow,Python,Tensorflow,我想根据我是在训练我的网络还是实际运行它,基本上有两个不同的图表。基本上,一个工件使用一些无监督的技术来学习给定矩阵的值,然后我想在不同的图中使用完全相同的矩阵 我知道如何使用matrix\u value=sess.run(my\u matrix,{input=input\u data})获取矩阵的值,但是有没有办法用设定值初始化tf.Variable import numpy as np import tensorflow as tf value = [0, 1, 2, 3, 4, 5, 6

我想根据我是在训练我的网络还是实际运行它,基本上有两个不同的图表。基本上,一个工件使用一些无监督的技术来学习给定矩阵的值,然后我想在不同的图中使用完全相同的矩阵


我知道如何使用
matrix\u value=sess.run(my\u matrix,{input=input\u data})获取矩阵的值
,但是有没有办法用设定值初始化
tf.Variable

import numpy as np
import tensorflow as tf

value = [0, 1, 2, 3, 4, 5, 6, 7]
init = tf.constant_initializer(value)

with tf.Session():
    x = tf.get_variable('x', shape=[2, 4], initializer=init)
    x.initializer.run()
    print(x.eval())

我希望这有帮助

您可以尝试以下方法:

import numpy as np
import tensorflow as tf

value = [0, 1, 2, 3, 4, 5, 6, 7]
init = tf.constant_initializer(value)

with tf.Session():
    x = tf.get_variable('x', shape=[2, 4], initializer=init)
    x.initializer.run()
    print(x.eval())

我希望这有帮助

您不必创建两个相同的图,您可以使用相同的节点,但运行不同的节点

让我解释一下我的意思。让我们看看这个例子:

W = tf.Variable(tf.truncated_normal([1, 1], stddev=0.1))
# Declare bias variable initialized to a constant 0.1
b = tf.Variable(tf.constant(0.1, shape=[1]))

y_pred = x_ph * W + b

# loss function
loss = tf.mul(tf.reduce_mean(tf.square(tf.sub(y_pred, y_ph))), 1. / 2)
第一部分调用
列操作

train_op = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
此op将基于
损失运行梯度下降步骤
op,这将导致变量
W
b
的更新

with tf.Session() as sess:
    # initialize all the variables by running the initializer operator
    sess.run(init)
    for epoch in xrange(num_epoch):
        # Run sequentially the train_op and loss operators with
        # x_ph and y_ph placeholders fed by variables x and y
        _, loss_val = sess.run([train_op, loss], feed_dict={x_ph: x, y_ph: y})
        print('epoch %d: loss is %.4f' % (epoch, loss_val))
但是现在,如果您只想运行它,您可以运行
y\u pred
op。它将拾取
W
b
的当前值,并且它们不会被修改,因为您没有调用
train\u op

with tf.Session() as sess:
    # see what model do in the test set
    # by evaluating the y_pred operator using the x_test data
    test_val = sess.run(y_pred, feed_dict={x_ph: x_test})
当您要求TensorFlow运行op
y\u pred
时,将新数据
x\u test
输入
x\u ph
,它将只计算
y\u pred=x\u ph*W+b
(将
W
b
作为常量),而不修改任何其他内容

此外,值得一提的是,当您完成培训时,您有能力覆盖某些变量值(例如,在变量值非常接近1的情况下,您可以直接根据设置将其设置为1)

我们可以通过将W和b的值重新指定为-1和1的完美值来手动改进这一点。变量初始化为提供给tf.variable的值,但可以使用tf.assign之类的操作进行更改。例如,W=-1和b=1是我们模型的最佳参数。我们可以相应地更改W和b:


您不必创建两个相同的图,您可以使用相同的节点,但运行不同的节点

让我解释一下我的意思。让我们看看这个例子:

W = tf.Variable(tf.truncated_normal([1, 1], stddev=0.1))
# Declare bias variable initialized to a constant 0.1
b = tf.Variable(tf.constant(0.1, shape=[1]))

y_pred = x_ph * W + b

# loss function
loss = tf.mul(tf.reduce_mean(tf.square(tf.sub(y_pred, y_ph))), 1. / 2)
第一部分调用
列操作

train_op = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
此op将基于
损失运行梯度下降步骤
op,这将导致变量
W
b
的更新

with tf.Session() as sess:
    # initialize all the variables by running the initializer operator
    sess.run(init)
    for epoch in xrange(num_epoch):
        # Run sequentially the train_op and loss operators with
        # x_ph and y_ph placeholders fed by variables x and y
        _, loss_val = sess.run([train_op, loss], feed_dict={x_ph: x, y_ph: y})
        print('epoch %d: loss is %.4f' % (epoch, loss_val))
但是现在,如果您只想运行它,您可以运行
y\u pred
op。它将拾取
W
b
的当前值,并且它们不会被修改,因为您没有调用
train\u op

with tf.Session() as sess:
    # see what model do in the test set
    # by evaluating the y_pred operator using the x_test data
    test_val = sess.run(y_pred, feed_dict={x_ph: x_test})
当您要求TensorFlow运行op
y\u pred
时,将新数据
x\u test
输入
x\u ph
,它将只计算
y\u pred=x\u ph*W+b
(将
W
b
作为常量),而不修改任何其他内容

此外,值得一提的是,当您完成培训时,您有能力覆盖某些变量值(例如,在变量值非常接近1的情况下,您可以直接根据设置将其设置为1)

我们可以通过将W和b的值重新指定为-1和1的完美值来手动改进这一点。变量初始化为提供给tf.variable的值,但可以使用tf.assign之类的操作进行更改。例如,W=-1和b=1是我们模型的最佳参数。我们可以相应地更改W和b:


它们不是完全相同的图。培训过程需要一个与我将用于实时分类的实际图完全不同的图(不是同构的)。我明白了,如果它们没有共享任何共同的子部分,那么显然不能重用它们。:)它们不是完全相同的图。培训过程需要一个完全不同的图(不同构)与我将用于实时分类的实际图形。我明白了,如果它们不共享任何子部分,那么显然您不能重用它们。:)