欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TypeError: __init__() got an unexpected keyword argument 'shape'

程序员文章站 2022-06-13 10:03:14
...

tensorFlow入门书籍中的代码

import tensorflow as tf

g1 = tf.Graph()
with g1.as_default():
    v = tf.get_variable("v", initializer=tf.zeros_initializer(shape=[1]))

g2 = tf.Graph()
with g2.as_default():
    v = tf.get_variable("v2", initializer=tf.ones_initializer(shape=[3,2])())

with tf.Session(graph=g1) as sess:
    tf.global_variables_initializer().run()
    with tf.variable_scope("", reuse=True):
        print(sess.run(tf.get_variable("v")))

with tf.Session(graph=g2) as sess:
    tf.global_variables_initializer().run()
    with tf.variable_scope("", reuse=True):
        print(sess.run(tf.get_variable("v2")))

运行的时候遇到如下问题

Traceback (most recent call last):
  File "C:/Users/Tobin/PycharmProjects/TF_study/test1.py", line 5, in <module>
    v = tf.get_variable("v", initializer=tf.zeros_initializer(shape=[1]))
TypeError: __init__() got an unexpected keyword argument 'shape

网上搜了下发现,最新版的TensorFlow已经不支持这样的写法了,应该讲代码改成

 v = tf.get_variable("v2", initializer=tf.ones_initializer()(shape=[3,2]))

那本《TensorFlow:实战Google深度学习框架》是比较早的一本书了,里面部分函数的的用法可能现在已经不支持了,所以要注意下

相关标签: tensorflow