关键词:
【中文标题】Tensorflow:用 tf.Variable 替换/提供图形的占位符?【英文标题】:Tensorflow: Replacing/feeding a placeholder of a graph with tf.Variable? 【发布时间】:2018-10-14 23:59:24 【问题描述】:我有一个模型M1
,其数据输入是占位符M1.input
,并且其权重经过训练。
我的目标是建立一个新模型M2
,它从w
的输入w
以tf.Variable
的形式计算M1
的输出o
(具有训练的权重)(而不是将实际值提供给@987654328 @)。换句话说,我使用训练好的模型M1
作为黑盒函数来构建一个新模型o = M1(w)
(在我的新模型中,w
是要学习的,M1
的权重固定为常数)。问题是M1
只接受M1.input
作为它的输入,我们需要通过它输入实际值,而不是像w
这样的tf.Variable。
作为构建M2
的简单解决方案,我可以在M2
内手动构建M1
,然后使用预训练值初始化M1
的权重,并使其在M2
内不可训练。然而,在实践中,M1
很复杂,我不想在M2
中再次手动构建M1
。我正在寻找一个更优雅的解决方案,例如解决方法或直接解决方案,用 tf.Variable w
替换 M1
的输入占位符 M1.input
。
感谢您的宝贵时间。
【问题讨论】:
【参考方案1】:这是可能的。怎么样:
import tensorflow as tf
def M1(input, reuse=False):
with tf.variable_scope('model_1', reuse=reuse):
param = tf.get_variable('param', [1])
o = input + param
return o
w = tf.get_variable('some_w', [1])
plhdr = tf.placeholder_with_default(w, [1])
output_m1 = M1(plhdr)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(w.assign([42]))
print(sess.run(output_m1, plhdr: [0])) # direct from placeholder
print(sess.run(output_m1)) # direct from variable
所以当 feed_dict 有占位符的值时,就会使用这个值。否则,使用变量“w”的后备选项处于活动状态。
【讨论】:
tensorflow中使用tf.variable_scope和tf.get_variable的valueerror
ValueError:Variableconv1/weights1alreadyexists,disallowed.Didyoumeantosetreuse=TrueinVarScope?Originallydefinedat:在使用tensorflow中的tf.variable_scope和tf.get_variable搭建网络时,重复运行程序会报以上的ValueError错误,这是因为第二次运 查看详情
tensorflow辨异——tf.placeholder与tf.variable
https://blog.csdn.net/lanchunhui/article/details/61712830https://www.cnblogs.com/silence-tommy/p/7029561.html二者的主要区别在于:tf.Variable:主要在于一些可训练变量(trainablevariables),比如模型的权重(weights,W)或者偏执值(bias);声明时,必须提 查看详情
莫烦tensorflow-variable
importtensorflowastfstate=tf.Variable(0,name=‘counter‘)one=tf.constant(1)new_value=tf.add(state,one)update=tf.assign(state,new_value)init=tf.initialize_all_variables()#musthaveifdefinevariablewithtf.S 查看详情
tensorflow辨异——tf.placeholder与tf.variable(代码片段)
二者的主要区别在于:tf.Variable:主要在于一些可训练变量(trainablevariables),比如模型的权重(weights,W)或者偏执值(bias);声明时,必须提供初始值;名称的真实含义,在于变量,也即在真实训练时,其值是会改变的,自... 查看详情
tensorflow中命名空间变量命名的问题(代码片段)
1.简介对比分析tf.Variable/tf.get_variable|tf.name_scope/tf.variable_scope的异同2.说明tf.Variable创建变量;tf.get_variable创建与获取变量tf.Variable自动检测命名冲突并且处理;tf.get_variable在没有设置reuse时会报错tf.name_scope没有reuse功能,tf.get_varia 查看详情
tensorflow变量variable(代码片段)
importtensorflowastfstate=tf.Variable(0,name=‘counter‘)#定义了名字是counter,初始值为0的计数器print(state.name)one=tf.constant(1)new_value=tf.add(state,one)update=tf.assign(state,new_value)print(new_value)init=tf.in 查看详情
tensorflow函数学习笔记
##tf.Variable(??) 创建tf变量##tf.matmul(w,x) 矩阵乘法w=tf.Variable([[0.5,1.0]])x=tf.Variable([[2.0],[1.0]])y=tf.matmul(w,x)init_op=tf.global_variables_initializer()withtf.Ses 查看详情
tensorflow-变量(代码片段)
...#-*-coding:utf-8-*-"""CreatedonWedDec1218:18:062018@author:myhaspl"""importtensorflowastftf.reset_default_graph()x1=tf.Variable(12.1,name="x1")x2=tf.Variable(18.22,name="x2")withtf.variable_scope("test1"):var1=tf.get_variable("x1",shape=(),dtype=tf.float32)withtf.variable_scope("test2"):var2=tf.get_... 查看详情
tensorflow的模型保存与加载
importosos.environ[‘TF_CPP_MIN_LOG_LEVEL‘]=‘2‘importtensorflowastf#tensorboard--logdir="./"deflinearregression():withtf.variable_scope("original_data"):X=tf.random_normal([100,1],mean=0.0,stddev=1.0)y_true=tf.matmul(X,[[0.8]])+[[0.7]]withtf.variable_scope("linear_model"):weights=tf.Variable(... 查看详情
tensorflow-chp04
#coding:utf-8importtensorflowastfif__name__=='__main__':(x,y),(x_test,y_test)=tf.keras.datasets.mnist.load_data()x=tf.convert_to_tensor(x,dtype=tf.float32)/255.y=tf.convert_to_tensor(y,dtype=tf.int32)y_onehot=tf.one_hot(y,depth=10)x=tf.reshape(x,[-1,28... 查看详情
简单神经网络tensorflow实现
学习TensorFlow笔记importtensorflowastf#定义变量#Variable定义张量及shapew1=tf.Variable(tf.random_normal([2,3],stddev=1,seed=1))w2=tf.Variable(tf.random_normal([3,1],stddev=1,seed=1))withtf.Session()assess:print(ses 查看详情
tensorflow梯度求解tf.gradients
importtensorflowastfw1=tf.Variable([[1,2]])w2=tf.Variable([[3,4]])res=tf.matmul(w1,[[2],[1]])grads=tf.gradients(res,[w1])withtf.Session()assess:tf.global_variables_initializer().run()p 查看详情
如何在 TensorFlow 中使用 tf.get_variable 和 numpy 值初始化变量?
】如何在TensorFlow中使用tf.get_variable和numpy值初始化变量?【英文标题】:Howdoesoneinitializeavariablewithtf.get_variableandanumpyvalueinTensorFlow?【发布时间】:2016-11-0612:27:49【问题描述】:我想用numpy值初始化我网络上的一些变量。为了这个... 查看详情
tensorflow——机器学习编程框架(代码片段)
TensorFlowTensorFlow是一个机器学习(即亦包括深度学习)的编程框架。Tensor张量张量是tensorflow计算中数据的基本单位,通过.shape获取形状,.dtype获取数值类型,.numpy()获取数值(将张量以numpy数组形式返回)。变量的域两种域(scope... 查看详情
tensorflow命令行参数flags使用
importosos.environ[‘TF_CPP_MIN_LOG_LEVEL‘]=‘2‘importtensorflowastf#tensorboard--logdir="./"#命令行参数pythonx.py--max_step=500tf.app.flags.DEFINE_integer("max_step",1000,"trainstepnumber")FLAGS=tf.app.flags.FLAGSdeflinearregression():withtf.variable_scope("original_data"):X=tf.random_no... 查看详情
tensorflow1.0officialbasement
tensorflow1.0新的赋值形式。W=tf.Variable([.3],tf.float32)b=tf.Variable([-.3],tf.float32)x=tf.placeholder(tf.float32)linear_model=W*x+bprint(linear_model,{x:[1,2,3,4]}) 查看详情
tensorflow--tensorflowboard(代码片段)
基本符号 代码:#-*-coding:UTF-8-*-#引入tensorflowimporttensorflowastf#构造图(Graph)的结构#用一个线性方程的例子y=W*x+bW=tf.Variable(2.0,dtype=tf.float32,name="Weight")#权重b=tf.Variable(1.0,dtype=tf.float32,name="Bias")#偏差x=tf.placeholder(dtype=tf.float32... 查看详情
tensorflow-variable(代码片段)
#!/usr/bin/envpython2#-*-coding:utf-8-*-"""CreatedonThuSep610:16:372018@author:myhaspl@email:[email protected]"""importtensorflowastfmy_var=tf.Variable(1)mutop=my_var.assign(my_var*3)init=tf.init 查看详情