Tensorflow
import tensorflow as tf
tf.add()
進行兩個變數的加減。 tf.math.add(x, y,
name=None) 1
2
3x = [1,2,3,4,5]
y = 2
print(tf.add(x,y))#output:tf.Tensor([3 4 5 6 7], shape=(5,), dtype=int32)1
2
3
4import tensorflow as tf
x = tf.convert_to_tensor([1,2,3,4,5])
y = tf.convert_to_tensor(2)
print(x+y)1
2
3
4import tensorflow as tf
x = tf.constant([1,2,3,4,5])
y = tf.constant([1,1,1,1,1])
tf.add(x,y)#output = <tf.Tensor: shape=(5,), dtype=int32, numpy=array([2, 3, 4, 5, 6], dtype=int32)>1
2
3
4
5def func(val):
val = tf.convert_to_tensor(val)
return val
v1 = func(tf.constant([[1,2,3],[4,5,6]]))
print(v1)1
2
3
4
5
6a = tf.constant([1,2,3,4,5])
b = tf.constant(5.5)
c = tf.constant("string")
print(a)
print(b)
print(c)1
2
3
4
5import tensorflow as tf
a = tf.ones(3,dtype = tf.int64)
b = tf.Variable(a,name = 'one')
print(a)
print(b)1
2
3
4
5
6
7
8
9
10
11import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
a = tf.Variable(5)
b = tf.Variable(10)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
print(sess.run(a))
print(sess.run(b))
sess.close()
接著下面那段程式碼是在進行輸出1~5。 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16import tensorflow.compat.v1 as tf
import numpy as np
tf.disable_v2_behavior()
state = tf.Variable(0,name = 'counter')
one = tf.constant(1)
new_value = tf.add(state,one)
update = tf.assign(state,new_value)
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
for i in range(5):
sess.run(update)
print(sess.run(state))
首先先看沒有Session()的時候宣告一個常量: 1
2
3
4
5
6import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
c = tf.constant(10)
print(c)#output:Tensor("Const_11:0", shape=(), dtype=int32)1
2
3
4
5
6
7
8import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
c = tf.constant(10)
sess = tf.Session()
print(sess.run(c))
sess.close()1
2
3
4
5
6
7
8import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
c = tf.constant(10)
with tf.Session() as sess:
c = sess.run(c)
print(c)1
2
3
4
5
6
7
8
9
10
11
12import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
m1 = tf.constant([[3,3]])
m2 = tf.constant([[2],[2]])
multi = tf.matmul(m1,m2)
sess = tf.Session()
output = sess.run(multi)
print(output)
sess.close()1
2
3
4
5
6
7
8
9
10
11import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
sess = tf.Session()
a = tf.constant([[1,10,1],[2,2,2]])
b = tf.reduce_mean(a)
c = tf.reduce_mean(a,1)
print(sess.run(b))# output = 3
print(sess.run(c))# output = [4 2]
sess.close()1
2
3
4
5
6
7
8
9
10import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
sess = tf.Session()
a = tf.constant([[1,2,3],[4,5,6]])
all = tf.reduce_sum(a)
second = tf.reduce_sum(a,1)
print(sess.run(all))
print(sess.run(second))
sess.close()1
2
3
4
5
6
7
8
9
10
11
12import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
sess = tf.Session()
a = tf.constant([1,2,3,4,5,6,7,8,9])
new_martix = tf.reshape(a,[3,-1])
print(sess.run(new_martix))
# output =
# [[1 2 3]
# [4 5 6]
# [7 8 9]]
sess.close()1
2
3
4
5
6
7
8import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
sess = tf.Session()
a = tf.constant([[48,24,64],[32,90,6]])
index = tf.argmin(a,1)
print(sess.run(index))# output [1 2]
sess.close()1
2
3
4
5
6
7
8
9import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
a = tf.Variable([1,2])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(a.eval())
sess.close()1
2
3
4
5
6
7
8
9
10import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
output = tf.multiply(x,y)
with tf.Session() as sess:
print(sess.run(output,feed_dict = {x:[10.],y:[7.]}))#output = [70.]