tensorflow-eagerAPI

调用该API可以不通过 tensorflow.Session.run()调用 定义的张量constant tensor,可以直接print

# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
import numpy as np
import tensorflow as tf
import tensorflow.contrib.eager as tfe

# 设置 eager API
tfe.enable_eager_execution()

a = tf.constant(2)
b = tf.constant(3)
print('a = %i' % a)
print('b = %i' % b)
# run op no tf.Session.run()
print("can run op without tf.Session.run")

c = a + b
c1 = a * b
print("no Session... c=%i" % c)
print("no Session... c1=%i" % c1)


# eagerAPI完全兼容numpy
# 定义张量 define constant tensors
a = tf.constant([[2., 1.],[1., 0]], dtype=tf.float32) # tensor
b = tf.constant([[3., 0.],[5., 1.]], dtype=tf.float32)
c2 = tf.matmul(a, b) # 矩阵相乘matmul

print("tensor:
 a=%s" % a)
print("tensor:
 b=%s" % b)
print("tensor multply :
 c2=%s" % c2)

print(a.shape[0]) # 多少组维度信息
print(a.shape[1]) # 维度

# tensor对象能够迭代? range  ?????
for i in range(a.shape[0]):
    for u in range(a.shape[1]):
        print(a[i][u])
原文地址:https://www.cnblogs.com/tangpg/p/9132304.html