1. 程式人生 > >Tensorflow學習教程------變量

Tensorflow學習教程------變量

cnblogs logs -1 glob sig clas 學習教程 cloc variables

  代碼

#coding:utf-8
import tensorflow as tf

x = tf.Variable([1,2])
a = tf.constant([3,3])
#增加一個減法op
sub = tf.subtract(x,a)
#增加一個加法op
add = tf.add(x,sub)
#有變量 一定要初始化 初始化所有的變量
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print(sess.run(sub))
    print(sess.run(add))

#自加操作 #創建一個變量初始化為0 state = tf.Variable(0,name=counter) #創建一個op 作用是使state加1 new_value = tf.add(state,1) #賦值操作 將new_value賦值給state update = tf.assign(state,new_value) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) print (sess.run(state)) for _ in range(5): sess.run(update)
print (sess.run(state))

  結果

name: GeForce GTX 1080 Ti
major: 6 minor: 1 memoryClockRate (GHz) 1.582
pciBusID 0000:03:00.0
Total memory: 10.91GiB
Free memory: 10.18GiB
I tensorflow/core/common_runtime/gpu/gpu_device.cc:906] DMA: 0 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:916] 0:   Y 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:975] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:03:00.0)
[
-2 -1] [-1 1] I tensorflow/core/common_runtime/gpu/gpu_device.cc:975] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:03:00.0) 0 1 2 3 4 5

Tensorflow學習教程------變量