python中错误TypeError: __init__() got an unexpected keyword argument 'shape'

学习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().run
    tf.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://blog.csdn.net/MissXy_/article/details/78597498

了解到是由于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]))

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据提供的引用内容,出现了TypeError: __init__() got an unexpected keyword argument 'date'和TypeError: __init__() got an unexpected keyword argument ‘encoding’的问题。这两个错误通常是由于使用了不支持的参数导致的。 对于第一个错误TypeError: __init__() got an unexpected keyword argument 'date',这个错误通常发生在调用某个函数或方法时传递了不支持的参数。要解决这个问题,你需要检查你的代码,确保你传递的参数是正确的,并且与函数或方法的定义相匹配。 对于第二个错误TypeError: __init__() got an unexpected keyword argument ‘encoding’,这个错误通常发生在使用json模块读取json文件时传递了不支持的参数。要解决这个问题,你需要检查你的代码,确保你传递的参数是正确的,并且与json模块的函数或方法的定义相匹配。 以下是两个示例来演示如何解决这两个错误: 1. 解决TypeError: __init__() got an unexpected keyword argument 'date'错误的示例: ```python class Person: def __init__(self, name): self.name = name person = Person(name='John', date='2021-01-01') # 错误的调用,传递了不支持的参数'date' ``` 在上面的示例,我们在实例化Person类时传递了一个不支持的参数'date',导致了TypeError: __init__() got an unexpected keyword argument 'date'错误。要解决这个错误,我们需要检查代码并删除不支持的参数'date'。 2. 解决TypeError: __init__() got an unexpected keyword argument ‘encoding’错误的示例: ```python import json with open('data.json', 'r', encoding='utf-8') as file: # 错误的调用,传递了不支持的参数'encoding' data = json.load(file) ``` 在上面的示例,我们在使用json模块读取json文件时传递了一个不支持的参数'encoding',导致了TypeError: __init__() got an unexpected keyword argument ‘encoding’错误。要解决这个错误,我们需要检查代码并删除不支持的参数'encoding'。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值