1. 程式人生 > >tensorflow官方文檔中的sub 和mul中的函數已經在API中改名了

tensorflow官方文檔中的sub 和mul中的函數已經在API中改名了

val import mod inpu 任務 初始化 問題: ria 學習

在照著tensorflow 官方文檔和極客學院中tensorflow中文文檔學習tensorflow時,遇到下面的兩個問題:

1)AttributeError: module ‘tensorflow‘ has no attribute ‘sub‘

#進入一個交互式Tensorflow會話
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.Variable([1.0,2.0])
a = tf.constant([3.0,3.0])
#使用初始化器initalizer op的run()方法初始化‘x‘
x.initializer.run()
#增加一個減法sub op, 從‘x‘減去‘a‘,運行減去op,輸出結果 sub = tf.sub(x,a) print(sub.eval()) # 任務完成,關閉回話 sess.close()

執行時報錯:

Traceback (most recent call last):
  File "C:/PythonProj/tensorflow/first_tensorflow.py", line 43, in <module>
    sub = tf.sub(x,a)
AttributeError: module tensorflow has no attribute sub

經過在pycharm中tf.自動反顯的信息,我發現原來這個sub函數已經被subtract代替了,換成tf.subtract(x,a) ,ok ,一切順利!

2)AttributeError: module ‘tensorflow‘ has no attribute ‘mul‘

input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)

intermed = tf.add(input2,input3)
mul = tf.mul(input1,intermed)
with tf.Session() as sess:
    result 
= sess.run([mul,intermed]) print(result)

報錯信息為:

Traceback (most recent call last):
  File "C:/PythonProj/tensorflow/first_tensorflow.py", line 78, in <module>
    mul = tf.mul(input1,intermed)
AttributeError: module tensorflow has no attribute mul

同理,經過在pycharm中tf.反顯信息的觀察,我發現原來這個tf.mul函數已經被換成了tf.multiply了,修改後,ok!

tensorflow官方文檔中的sub 和mul中的函數已經在API中改名了