tensorflow2报错AttributeError: module ‘tensorflow‘ has no attribute ‘truncated_normal‘

tensorflow2报错AttributeError: module ‘tensorflow’ has no attribute ‘truncated_normal’

代码
tn = tf.truncated_normal([prev_num_nodes, num_nodes], stddev=w_stddev)
报错信息
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-14-05dbe3f468af> in <module>()
      5 
      6 # new_alex = TransferedAlexNet(old_alex_graph, num_tgt_classes=2)
----> 7 new_alex = TransferedAlexNet(old_alex_graph)
      8 print(new_alex)
      9 inputs   = old_alex_graph.get_tensor_by_name('inputs/images:0')

<ipython-input-12-d49ea2971fb0> in __init__(s, init_graph)
     21             with tf.name_scope('new_top_layer'):
     22                 # the old fc8 (which we are replacing) had 1000 nodes for 1000 classes
---> 23                 s.logits = helpers_07.fully_connected_xavier_relu_layer(orig_fc7)
     24                 print( s.logits)
     25 

 in fully_connected_xavier_relu_layer(incoming_layer, num_nodes, b_val, keep_prob, name)
     84                                  activation_fn = tf.nn.relu,
     85                                  keep_prob=keep_prob,
---> 86                                  name=name)
     87 
     88 if __name__ == "__main__":

in fully_connected_layer(incoming_layer, num_nodes, w_stddev, b_val, activation_fn, keep_prob, name)
     56         # FIXME:  consider stddev=np.sqrt(2.0 / prev_num_nodes)
     57         #         [fancy initialization]
---> 58         tn = tf.truncated_normal([prev_num_nodes, num_nodes], stddev=w_stddev)
     59         W = tf.Variable(tn, name='W')
     60         const = tf.constant(b_val, shape=[num_nodes])

AttributeError: module 'tensorflow' has no attribute 'truncated_normal'



解决方法

tf.random.truncated_normal([prev_num_nodes, num_nodes], stddev=w_stddev)

  • 15
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
### 回答1: 这个错误通常是因为Tensorflow的版本问题导致的。在Tensorflow 2.0及以上的版本中,truncated_normal已经被移除了,取而代之的是TruncatedNormal函数。如果你正在使用Tensorflow 1.x版本,你可以尝试使用以下代码修复这个问题: ``` import tensorflow as tf tf.compat.v1.truncated_normal() ``` 如果你使用的是Tensorflow 2.0或更高版本,你可以使用以下代码解决这个问题: ``` import tensorflow as tf tf.random.truncated_normal() ``` ### 回答2: 您好,AttributeError: module 'tensorflow' has no attribute 'truncated_normal' 是由于 TensorFlow 模块中没有 truncated_normal 方法所引起的。 在 TensorFlow 2.x 版本中,truncated_normal 方法已被移除,取而代之的是 tf.random.truncated_normal 方法,用于生成截断的正态分布随机数。 例如,您可以使用以下代码来生成截断的正态分布随机数: import tensorflow as tf random_tensor = tf.random.truncated_normal(shape=[2, 3], mean=0.0, stddev=1.0) 其中,shape 参数指定了生成随机数的形状,mean 参数指定了正态分布的均值,stddev 参数指定了正态分布的标准差。 希望以上信息能够回答您的问题。如有任何疑问,请随时向我提问。 ### 回答3: 在TensorFlow中,`truncated_normal`函数是用于创建具有截断正态分布的张量的函数。在Python中,当我们尝试使用`truncated_normal`函数时,可能会遇到`AttributeError: module 'tensorflow' has no attribute 'truncated_normal'`的错误。 这个错误通常意味着我们正在使用一个不支持被调用的函数或方法。在TensorFlow的较新版本中,`truncated_normal`函数已被弃用并替换为`tf.random.truncated_normal`函数。 为了解决这个问题,我们需要使用新的函数名称`tf.random.truncated_normal`来创建具有截断正态分布的张量。我们可以使用以下代码示例来创建一个具有截断正态分布的张量: ```python import tensorflow as tf shape = [3, 3] mean = 0.0 stddev = 1.0 tensor = tf.random.truncated_normal(shape, mean, stddev) ``` 在这个例子中,我们导入了`tensorflow`库并将我们想要的张量形状、平均值和标准差作为参数传递给`tf.random.truncated_normal`函数。这将创建一个具有截断正态分布的张量。 总之,当使用TensorFlow时,如果遇到`AttributeError: module 'tensorflow' has no attribute 'truncated_normal'`错误,我们应该使用`tf.random.truncated_normal`函数来替代`truncated_normal`函数来创建具有截断正态分布的张量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值