python中错误TypeError: __init__() got an unexpected keyword argument 'shape'
2018-07-20 13:44
2466 查看
学习TensorFlow遇到的问题:
1 如何在不同计算图上定义和使用变量
书上代码:
import tensorflow as tf g1 = tf.Graph() with g1.as_default(): #在计算g1中定义变量“v”,并设置初始值为0。 v = tf.get_variable( "v", initializer=tf.zeros_initializer(shape=[1]) ) g2 = tf.Graph() with g2.as_default(): #在计算图g2中定义变量"v",并设置初始值为1. v=tf.get_variable( "v", initializer=tf.ones_initializer(shape=[1]) ) #在计算图g1中读取变量“v”的取值。 with tf.Session(graph=g1) as sess: #tf.initialize_all_variables().runtf.global_variables_initializer().run() with tf.variable_scope("",reuse=True): #在计算图g1中,变量“v”的取值应该为0,所以下面这行会输出[0.] print(sess.run(tf.get_variable("v"))) #在计算图g2中读取变量”v“的取值 with tf.Session(graph=g2) as sess: tf.initialize_all_variables().run() with tf.variable_scope("",reuse=True): #在计算图g2中,变量”v“的取值应该为1,所以下面这行会输出[1.] print(sess.run(tf.get_variable("v")))
报错:
故在网上找了原因:
参考博客https://www.geek-share.com/detail/2722273076.html
了解到是由于python版本的问题
错误更改:
v = tf.get_variable("v", shape=[1], initializer=tf.zeros_initializer)
虽然输出了结果但是会有警告如下图:
警告更改:
tf.global_variables_initializer().run()
参照 博客下面的评论了解到是由于新版tf.zeros_initializer和ones_initializer后面需要加括号
tf.get_variable(“v”,initializer=tf.zeros_initializer(shape=[1]))
改为:
v = tf.get_variable(“v”,initializer=tf.zeros_initializer( )(shape=[1]))
阅读更多
相关文章推荐
- TypeError: __init__() got an unexpected keyword argument 'shape'
- Python openpyxl TypeError: __init__() got an unexpected keyword argument 'pivotButton' (and)
- TypeError: __init__() got an unexpected keyword argument 'shape'
- pip错误:TypeError: parse() got an unexpected keyword argument 'transport_encoding'
- pip错误:TypeError: parse() got an unexpected keyword argument 'transport_encoding'
- RESTful学习笔记 --- TypeError: __init__() got an unexpected keyword argument 'method'
- pip错误:TypeError: parse() got an unexpected keyword argument 'transport_encoding'
- TypeError:__init__() got an unexpected keyword argument 'xxx'
- TypeError: __init__() got an unexpected keyword argument ‘maxlength’
- tensorflow 1.5 TypeError: __init__() got an unexpected keyword argument 'dct_method'
- TypeError: __init__() got an unexpected keyword argument ‘maxlength’
- TypeError: __init__() got an unexpected keyword argument ‘default’
- python pandas 报错:TypeError: parser_f() got an unexpected keyword argument 'skip_blank_lines'
- pip错误:TypeError: parse() got an unexpected keyword argument 'transport_encoding'
- pip错误:TypeError: parse() got an unexpected keyword argument 'transport_encoding'
- pip错误:TypeError: parse() got an unexpected keyword argument 'transport_encoding'
- 《Flask Web开发》学习笔记之bug--(6)【TypeError: __init__() got an unexpected keyword argument 'method'】
- 【Python配置】TypeError: parse() got an unexpected keyword argument 'transport_encoding'
- TypeError: __init__() got an unexpected keyword argument ‘maxlength’
- TypeError: __init__() got an unexpected keyword argument 'syntax'