TensorFlow 运行方法
import tensorflow as tf
m1 = tf.constant([[2, 2]])
m2 = tf.constant([[3],
[3]])
dot_operation = tf.matmul(m1, m2)
print(dot_operation) # wrong! no result
# method1 use session
sess = tf.Session()
result = sess.run(dot_operation)
print(result)
sess.close()
# method2 use session
with tf.Session() as sess:
result_ = sess.run(dot_operation)
print(result)
import tensorflow as tf
state = tf.Variable(0,name='counter')
# print(state.name)
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 _ in range(3):
sess.run(update)
print(sess.run(state))
带变量的时候必须要加 ‘init = tf.initialize_all_variables()’
###placeholder传入值
import tensorflow as tf
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1,input2)
with tf.Session() as sess:
print(sess.run(output,feed_dict={input1:[7.],input2:[3.]}))
python 基础知识
1.列表:list []
Python内置的一种数据类型是列表list是一种有序的集合,可以随时添加和删除其中的元素。
>>> classmates = ['Michael', 'Bob', 'Tracy']
>>> classmates
['Michael', 'Bob', 'Tracy'] #索引是从0开始的
classmates.insert(1, 'Jack')
2.元组:tuple ()
另一种有序列表叫元组:tuple。tuple和list非常类似,但是tuple一旦初始化就不能修改,比如同样是列出同学的名字:
>>> classmates = ('Michael', 'Bob', 'Tracy')
classmates这个tuple不能变了,它也没有append(),insert()这样的方法。其他获取元素的方法和list是一样的,正常地使用classmates[0],去获取元素
>>> t = ('a', 'b', ['A', 'B'])
>>> t[2][0] = 'X'
>>> t[2][1] = 'Y'
>>> t
('a', 'b', ['X', 'Y'])
3.字典:dict {}
dict的实现原理和查字典是一样的,使用键-值(key-value)存储
>>> d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}
>>> d['Michael']
95