美文网首页
ts 入门01 基础语法

ts 入门01 基础语法

作者: ccccca | 来源:发表于2018-10-17 18:13 被阅读0次
import tensorflow as tf

# 创建常量
hello = tf.constant('Hello, TensorFlow!')

# 开启会话
sess = tf.Session()

# hello tensorflow
print(sess.run(hello))

import numpy as np

import tensorflow as tf

sess = tf.Session()

# 定义tensorflow常量

print("Define constant tensors")

a = tf.constant(2)

print("a = %i" % sess.run(a))

b = tf.constant(3)

print("b = %i" % sess.run(b))

# 运行加减运算

print("Running operations, without tf.Session")

c = a + b

print("a + b = %i" % sess.run(c))

d = a * b

print("a * b = %i" % sess.run(d))

# Full compatibility with Numpy
print("Mixing operations with Tensors and Numpy Arrays")

# tf方式和np方式定义2x2数组
a = tf.constant([[2., 1.],
                 [1., 0.]], dtype=tf.float32)
print("Tensor:\n a = %s" % sess.run(a))
b = np.array([[3., 0.],
              [5., 1.]], dtype=np.float32)
print("NumpyArray:\n b = %s" % b)

# 对矩阵进行运算处理
print("Running operations, without tf.Session")

c = a + b
print("a + b = %s" % sess.run(c))

d = tf.matmul(a, b)
print("a * b = %s" % sess.run(d))

print("Iterate through Tensor 'a':")
for i in range(a.shape[0]):
    for j in range(a.shape[1]):
        print(sess.run(a[i][j]))

相关文章

网友评论

      本文标题:ts 入门01 基础语法

      本文链接:https://www.haomeiwen.com/subject/cgwrzftx.html