TensorFlow基础笔记(0) tensorflow的基本数据类型操作

 

 

import numpy as np
import tensorflow as tf

#build a graph
print("build a graph")
#生产变量tensor
a=tf.constant([[1,2],[3,4]])
b=tf.constant([[1,1],[0,1]])
#获取tensor的数据类型和张量维度
print("a.dtype",a.dtype)
print(a.get_shape())
print("type of a:",type(a))

#基本的数据运算
c=tf.matmul(a,b)
d=tf.subtract(a,b)
e=tf.add(a,b)
print("a:",a)
print("b:",b)
#construct a 'Session' to excute the graph
sess=tf.Session()
# Execute the graph and store the value that `c` represents in `result`.
print("excuted in Session")
result_a=sess.run(a)
result_a2=a.eval(session=sess)
print("result_a:\n",result_a)
print("result_a2:\n",result_a2)

result_b=sess.run(b)
print("result_b:\n",result_b)

result_c=sess.run(c)
print("result_c:\n",result_c)

result_d=sess.run(d)
print("result_d:\n",result_d)

result_e=sess.run(e)
print("result_e:\n",result_e)

 

#Tensors常量值函数
tf.zeros(shape, dtype=tf.float32, name=None)
tf.zeros_like(tensor, dtype=None, name=None)
tf.ones(shape, dtype=tf.float32, name=None)
tf.ones_like(tensor, dtype=None, name=None)
tf.fill(dims, value, name=None)
tf.constant(value, dtype=None, shape=None, name='Const')

posted on 2018-03-20 13:25  Maddock  阅读(518)  评论(0编辑  收藏  举报

导航