-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf_begin
82 lines (63 loc) · 2.09 KB
/
tf_begin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
import tensorflow as tf
# Create a constant op that produces a 1x2 matrix. The op is
# added as a node to the default graph.
#
# The value returned by the constructor represents the output
# of the constant op.
matrix1 = tf.constant([[3., 3.]])
# Create another constant that produces a 2x1 matrix.
matrix2 = tf.constant([[2.],[2.]])
# Create a matmul op that takes 'matrix1' and 'matrix2' as inputs.
# The returned value, 'product', represents the result of the matrix
# multiplication.
product = tf.matmul(matrix1, matrix2)
print(product)
# Launch the default graph.
#上記に書いた演算をここで初めて実行する。
sess = tf.Session()
# To run the matmul op we call the session 'run()' method, passing 'product'
# which represents the output of the matmul op. This indicates to the call
# that we want to get the output of the matmul op back.
#
# All inputs needed by the op are run automatically by the session. They
# typically are run in parallel.
#
# The call 'run(product)' thus causes the execution of three ops in the
# graph: the two constants and matmul.
#
# The output of the matmul is returned in 'result' as a numpy `ndarray` object.
result = sess.run(product) #get result of the session; product
print(result)
sess.close()
with tf.Session() as sess:
result = sess.run([product])
print(result)
#Variables
state=tf.Variable(0,name="counter")
one=tf.constant(1)
new_value=tf.add(state,one)
update=tf.assign(state,new_value)
init_op=tf.global_variables_initializer()
#Launch graphs
with tf.Session() as sess:
sess.run(init_op)
print(sess.run(state))
for _ in range(3):
sess.run(update)
print(sess.run(state))
#Fetches
input1=tf.constant([3.0])
input2=tf.constant([2.0])
input3=tf.constant([5.0])
intermed=tf.add(input2,input3)
mul=tf.multiply(input1,intermed)
with tf.Session() as sess:
result=sess.run([mul,intermed])
print(result)
#Feeds
input1=tf.placeholder(tf.float32)
input2=tf.placeholder(tf.float32)
output=input1*input2
with tf.Session() as sess:
print(sess.run([output],feed_dict={input1:[7.], input2:[2.]}))