以下代码导致一个非常无用的错误:
import tensorflow as tf
x = tf.Variable(tf.constant(0.), name="x")
with tf.Session() as s:
val = s.run(x.assign(1))
print(val) # 1
val = s.run(x, {x: 2})
print(val) # 2
val = s.run(x.assign(1), {x: 0.}) # InvalidArgumentError
节点Assign_1的tensorflow.p
我很难在Tensorflow的C++ API中找到一种初始化变量的工作方法。我试图为MNIST数据集制作一个基本的完全连接的单层神经网络。
目前,我发现正确的方法是这样做:
TF_CHECK_OK(session->Run({}, {}, {"init_all_vars_op"}, nullptr));
但是,使用此方法将返回以下错误:
tensorflow/cc/example/example.cc:178:71: error: no matching function for call to 'tensorflow::ClientSession::Run(&l
我已经使用python-tensorflow训练了一个模型,我想在java-tensorflow中进行推断。我已经将训练好的模型/图加载到Java中。在此之后,我想永久更新图中的一个变量。我知道python中的tf.variable.load(value,session)函数可以用来更新变量的值。我想知道Java中是否有类似的方法。
到目前为止,我已经尝试了以下几种方法。
// g and s are loaded graphs and sessions respectively
s.runner().feed(variableName,updatedTensorValue)
但上面的代码行仅
我试图理解为什么其中一个实现可以工作,而另一个实现不能工作。我试图用tensorflow表示一些几何。
首先,一个助手文件,d_math.py
!/usr/bin/env python3
进口numpy作为np进口tensorflow作为tf
dtype = tf.float64
def skew_symmetric(vector):
#Creates a tensorflow matrix which is a skew-symmetric version of the input vector
return tf.stack([(0., -vector[2], ve
运行下面的测试
from unittest import TestCase
import tensorflow as tf
class TestControl(TestCase):
def test_control_dep(self):
print(tf.__version__)
a = tf.get_variable('a', initializer=tf.constant(0.0))
d_optim = tf.assign(a, a + 2)
g_optim = tf.assign(a, a * 2)
with tf.con
我试图理解tf.assign和赋值运算符(=)之间的区别。我有三组代码
首先,使用简单的tf.assign
import tensorflow as tf
with tf.Graph().as_default():
a = tf.Variable(1, name="a")
assign_op = tf.assign(a, tf.add(a,1))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print sess.run(assign_op)
p
在Tensorflow 1.9中,我希望创建一个网络,然后递归地将网络的输出(预测)反馈到网络的输入中。在这个循环中,我希望将网络所做的预测存储在一个列表中。
以下是我的尝试:
# Define the number of steps over which to loop the network
num_steps = 5
# Define the network weights
weights_1 = np.random.uniform(0, 1, [1, 10]).astype(np.float32)
weights_2 = np.random.u
所以我尝试了以下几种方法
x = tf.Variable(0.10, tf.float32)
tf.assign(x, tf.add(x,1))
和
x = tf.Variable(0.10, tf.float32)
x = x + 1
但它们不管用。您知道我们如何在TensorFlow中执行这样的功能吗?
完整代码
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.Variable(0.10, tf.float32)
y = tf.constant(1.00, tf.float32)
x.assign(1.0)
ses
import tensorflow as tf
def f(x):
return tf.multiply(x, x)
x = tf.Variable([3.])
with tf.GradientTape() as test_tape:
test_tape.watch(x)
with tf.GradientTape() as train_tape:
train_tape.watch(x)
fx = f(x)
gradient = train_tape.gradient(fx, x) # df(x)/x = d(x^2)
我在Tensorflow中创建了一个简单的数组变量,并且我正在尝试找到一种方法来多次执行赋值。我知道为了在TF中进行赋值,你需要使用tf.assign函数。问题是它返回的是“切片类型”,而不是变量类型。
代码示例:
a = [-1.2, -5, 30.0, -7.5, 0.75]
v = tf.get_variable("v", shape=[5], initializer=tf.constant_initializer(a))
s = tf.Session()
c = c[0].assign(55) # Now c is not a regular variable anym
试图运行以下基本示例来运行条件计算,我得到了以下错误消息:
'x‘传递的浮点数与预期的float_ref不兼容
什么是tensorflow float_ref,以及如何修改代码?
import tensorflow as tf
from tensorflow.python.ops.control_flow_ops import cond
a = tf.Variable(tf.constant(0.),name="a")
b = tf.Variable(tf.constant(0.),name="b")
x = tf.Variable(tf.co
在下面的简单代码中,梯度得到了正确的计算。
import tensorflow as tf
x = tf.constant([1, 2, 3, 4], dtype=tf.float32)
y = tf.Variable(tf.ones_like(x), dtype=tf.float32)
y = 2*x
grad = tf.gradients(y, x)
ini = tf.global_variables_initializer()
with tf.Session() as ses:
ses.run(ini)
print(ses.run(grad))
正如预期的那样,
我不明白为什么在一次运行调用中运行变量初始化以及赋值方法时,值不被赋值?是与并行执行有关,还是没有操作优先级?TF 不能解释这一点。
示例:
import tensorflow as tf
W = tf.Variable(10)
with tf.Session() as sess:
sess.run([W.initializer, W.assign(20)])
print W.eval() #
>> returns 10, but I would expect 20
#running it separately:
sess.run(W.initi
我正在尝试恢复here提供的预训练模型,并在不同的数据集上继续训练。那里可用的预训练模型是在tensorflow_gpu-1.1.0上训练的。但我有tensorflow_gpu-1.13.1。当我尝试恢复模型时,我得到了下面的错误。 NotFoundError (see above for traceback): Restoring from checkpoint failed. This is most likely due to a Variable name or other graph key that is missing from the checkpoint. Please e
我对Tensorflow非常陌生,但我的最终目标是实现Tensorflow 2.0中“使用快速权重关注最近的过去”中描述的体系结构。我从一个来自的拷贝开始,我的想法是简单地将这个SimpleRNN (层)扩展到我所需要的。这个扩展不会花费太多的精力,因为它只包括
( a)创建单元格时初始化矩阵(使用零),
( b)在调用()函数中手动更新此矩阵。(这是通过简单地构建递归层激活的输出积并将其添加到所述矩阵中来实现的),
( c)将其添加到正常的储层活动中。
我的问题是,在尝试手动更新此矩阵时,会出现以下错误:
TypeError: An op outside of the function bu
我正在尝试在Tensorflow中实现体验回放。我遇到的问题是存储模型试验的输出,然后同时更新梯度。我尝试过的几种方法是存储来自sess.run(模型)的结果值,然而,这些不是张量,并且不能用于tensorflow所涉及的梯度下降。我目前正在尝试使用tf.assign(),然而,我遇到的困难在这个例子中得到了最好的体现。
import tensorflow as tf
import numpy as np
def get_model(input):
return input
a = tf.Variable(0)
b = get_model(a)
d = tf.Variable(0)