在线安装
离线安装
修改源进行安装。如果安装包time out 错误,则可以修改pip源,重新进行安装,修改方式:
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com
sudo pip3 --timeout 600
install tensorflow-1.14.0-cp35-cp35m-manylinux1_x86_64.whl
# tensorflow版本的helloworld
import tensorflow as tf
# TF1.x hello world
# hello = tf.constant("Hello, world!") # 定义一个常量(张量)
# sess = tf.Session() # 创建一个session,用来执行操作
# print(sess.run(hello)) # 调用session的run方法,执行hello操作,并打印结果
# sess.close() # 关闭session
# TF2.x hello world
# msg = tf.constant('Hello, world!')
# tf.print(msg)
# TF2.x -> TF1.x hello world
tf.compat.v1.disable_eager_execution()
msg = tf.constant('Hello, world!')
sess = tf.compat.v1.Session()
print(sess.run(msg))
# 张量相加的示例
import tensorflow as tf
# TF1.x
a = tf.constant(5.0) # 张量a
b = tf.constant(1.0) # 张量b
c = tf.add(a, b) # 张量相加
with tf.Session() as sess:
print(sess.run(c))
# TF2.x -> TF1.x
tf.compat.v1.disable_eager_execution()
a = tf.constant(5.0) # 张量a
b = tf.constant(1.0) # 张量b
c = tf.add(a, b) # 张量相加
with tf.compat.v1.Session() as sess:
print(sess.run(c))
"""
6.0
"""




# 查看默认图的属性
import tensorflow as tf
# TF1.x
a = tf.constant(5.0)
b = tf.constant(1.0)
c = tf.add(a, b)
graph = tf.get_default_graph() # 获取默认的图
print(graph)
with tf.Session() as sess:
print(sess.run(c))
print(a.graph) # 打印张量的graph属性
print(c.graph) # 打印c操作的graph属性
print(sess.graph) # 打印session的graph属性
# TF2.x -> TF1.x
tf.compat.v1.disable_eager_execution()
a = tf.constant(5.0)
b = tf.constant(1.0)
c = tf.add(a, b)
graph = tf.compat.v1.get_default_graph() # 获取默认的图
print(graph)
with tf.compat.v1.Session() as sess:
print(sess.run(c))
print(a.graph) # 打印张量的graph属性
print(c.graph) # 打印c操作的graph属性
print(sess.graph) # 打印session的graph属性
"""
6.0
"""
# 查看默认图的属性
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
a = tf.constant(5.0)
b = tf.constant(1.0)
c = tf.add(a, b)
graph = tf.compat.v1.get_default_graph() # 获取默认的图
print("graph:", graph)
# 新创建一个图
graph2 = tf.Graph()
print("graph2:", graph2)
with graph2.as_default(): # 设置为默认图
d = tf.constant(11.0) # 操作d属于graph2
with tf.compat.v1.Session() as sess:
print(sess.run(c))
print(sess.run(d)) # 报错,因为d没有在默认的graph中
print(a.graph) # 打印张量的graph属性
print(c.graph) # 打印c操作的graph属性
print(sess.graph) # 打印session的graph属性
"""
ValueError: Argument `fetch` = Tensor("Const:0", shape=(), dtype=float32)
cannot be interpreted as a Tensor. (Tensor Tensor("Const:0", shape=(), dtype=float32)
is not an element of this graph.)
"""
# 查看默认图的属性
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
a = tf.constant(5.0)
b = tf.constant(1.0)
c = tf.add(a, b)
graph = tf.compat.v1.get_default_graph() # 获取默认的图
print("graph:", graph)
# 新创建一个图
graph2 = tf.Graph()
print("graph2:", graph2)
with graph2.as_default(): # 设置为默认图
d = tf.constant(11.0) # 操作d属于graph2
with tf.compat.v1.Session(graph=graph2) as sess: # 指定执行graph2
# print(sess.run(c)) # 报错,因为c没有在默认的graph中
print(sess.run(d))
print(a.graph) # 打印张量的graph属性
print(c.graph) # 打印c操作的graph属性
print(sess.graph) # 打印session的graph属性
"""
graph:
graph2:
11.0
"""

| 属性名称 | 说明 |
|---|---|
| graph | 所属的默认图 |
| op | 张量的操作名 |
| name | 名称 |
| shape | 形状 |
| dtype | 元素类型 |
# 查看张量属性示例
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
a = tf.constant(5.0) # 标量
with tf.compat.v1.Session() as sess:
print(sess.run(a))
print("name: ", a.name) # name属性
print("dtype: ", a.dtype) # dtype属性
print("shape: ", a.shape) # shape属性
print("op: ", a.op) # op属性
print("graph: ", a.graph) # graph属性
"""
5.0
name: Const:0
dtype:
shape: ()
op: name: "Const"
op: "Const"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_FLOAT
tensor_shape {
}
float_val: 5.0
}
}
}
graph:
"""
# 创建张量示例
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
# 创建值全为0的张量
tensor_zeros = tf.zeros(shape=[2, 3], # 2行3列
dtype="float32") # 类型
# 创建值全为1的张量
tensor_ones = tf.ones(shape=[2, 3],
dtype="float32")
# 创建正态分布随机张量
tensor_nd = tf.compat.v1.random_normal(shape=[10], # 一维,10个元素
mean=1.7, # 中位数
stddev=0.2,
dtype="float32")
# 创建形状和tensor_ones一样,值全为0的张量
tensor_zeros_like = tf.zeros_like(tensor_ones)
with tf.compat.v1.Session() as sess:
print(tensor_zeros.eval()) # eval表示在session中执行计算
print(tensor_ones.eval())
print(tensor_nd.eval())
print(tensor_zeros_like.eval())
"""
[[0. 0. 0.]
[0. 0. 0.]]
[[1. 1. 1.]
[1. 1. 1.]]
[1.5594195 1.5061207 1.6503117 1.4521958 1.8483374 1.7134072 1.8214194
1.7255555 1.7541615 1.4169383]
[[0. 0. 0.]
[0. 0. 0.]]
"""
| 函数名称 | 说明 |
|---|---|
| tf.string_to_number(string_tensor) | 字符串转换为数字 |
| tf.to_double(x) | 转换为64位浮点型 |
| tf.to_float(x) | 转换为32位浮点型 |
| tf.to_int32(x) | 转换为32位整型 |
| tf.to_int64(x) | 转换为64位整型 |
| tf.cast(x, dtype) | 将x转换为dtype所指定的类型 |
# 张量类型转换示例
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
tensor_ones = tf.ones(shape=[2, 3], dtype="int32")
tensor_float = tf.constant([1.1, 2.2, 3.3])
with tf.compat.v1.Session() as sess:
print(tf.cast(tensor_ones, tf.float32).eval()) # 将tensor_ones转换为浮点型并打印
"""
[[1. 1. 1.]
[1. 1. 1.]]
"""
# 占位符使用示例:占位符在使用时,必须传入参数
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
# 定义两个占位符
plhd = tf.compat.v1.placeholder(tf.float32, [2, 3]) # 定义2行3列的占位符
plhd2 = tf.compat.v1.placeholder(tf.float32, [None, 3]) # N行3列的占位符
plhd3 = tf.compat.v1.placeholder(tf.float32, [None, 4]) # N行4列的占位符
with tf.compat.v1.Session() as sess:
d = [[1, 2, 3],
[4, 5, 6]]
print(sess.run(plhd, feed_dict={plhd: d})) # 执行占位符操作,需要传入数据
print(sess.run(plhd2, feed_dict={plhd2: d})) # 定义为N行3列,执行时传入2行3列
# print(sess.run(plhd2, feed_dict={plhd3: d})) # 定义为N行4列,执行时传入2行3列
"""
[[1. 2. 3.]
[4. 5. 6.]]
[[1. 2. 3.]
[4. 5. 6.]]
"""
# 张量形状改变
# 静态形状:初始形状,只能设置一次,不能跨阶设置
# 动态形状:运行时的形状,可以多次设置,可以跨阶设置,但元素总数要一致
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
pld = tf.compat.v1.placeholder(tf.float32, [None, 3])
pld.set_shape([4, 3]) # 设置静态形状,一旦固定就不能再改变
print(pld)
# pld.set_shape([3, 3]) # 报错
# 设置张量的动态形状,实际是创建一个新的张量
new_pld = tf.reshape(pld, [3, 4]) # 设置动态形状
print(new_pld)
new_pld = tf.reshape(pld, [2, 6]) # 多次设置动态形状
print(new_pld)
# new_pld = tf.reshape(pld, [2, 4]) # 报错,元素个数不匹配
with tf.compat.v1.Session() as sess:
pass
"""
Tensor("Placeholder:0", shape=(4, 3), dtype=float32)
Tensor("Reshape:0", shape=(3, 4), dtype=float32)
Tensor("Reshape_1:0", shape=(2, 6), dtype=float32)
"""