正常情况下,使用tf.initialize_all_variables()初始化变量,在完全构建好模型并加载之后才运行这个操作。生成数据的主要方法如下
1)如果需要利用已经初始化的参数给其他变量赋值
TF的变量有个initialized_value()属性,就是初始化的值,使用方法如下:
1 2 3 4 5 6 |
# 原始的变量
weights
=
tf
.
Variable
(
tf
.
random_normal
([
784
,
200
],
stddev
=
0.35
),
name
=
"weights"
)
# 创造相同内容的变量
w2
=
tf
.
Variable
(
weights
.
initialized_value
(),
name
=
"w2"
)
# 也可以直接乘以比例
w_twice
=
tf
.
Variable
(
weights
.
initialized_value
()
*
0.2
,
name
=
"w_twice"
)
|
生成tensor:
tf.zeros(shape, dtype=tf.float32, name=None)
tf.zeros_like(tensor, dtype=None, name=None)
tf.constant(value, dtype=None, shape=None, name='Const')
tf.fill(dims, value, name=None)
tf.ones_like(tensor, dtype=None, name=None)
tf.ones(shape, dtype=tf.float32, name=None)
生成序列
tf.range(start, limit, delta=1, name='range')
tf.linspace(start, stop, num, name=None)
生成随机数
tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
tf.random_uniform(shape, minval=0.0, maxval=1.0, dtype=tf.float32, seed=None, name=None)
tf.random_shuffle(value, seed=None, name=None)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import
tensorflow
as
tf
import
numpy
as
np
# 生成0和1矩阵
v1
=
tf
.
Variable
(
tf
.
zeros
([
3
,
3
,
3
]),
name
=
"v1"
)
v2
=
tf
.
Variable
(
tf
.
ones
([
10
,
5
]),
name
=
"v2"
)
#填充单值矩阵
v3
=
tf
.
Variable
(
tf
.
fill
([
2
,
3
],
9
))
#常量矩阵
v4_1
=
tf
.
constant
([
1
,
2
,
3
,
4
,
5
,
6
,
7
])
v4_2
=
tf
.
constant
(
-
1.0
,
shape
=
[
2
,
3
])
#生成等差数列
v6_1
=
tf
.
linspace
(
10.0
,
12.0
,
30
,
name
=
"linspace"
)
#float32 or float64
v7_1
=
tf
.
range
(
10
,
20
,
3
)
#just int32
#生成各种随机数据矩阵
v8_1
=
tf
.
Variable
(
tf
.
random_uniform
([
2
,
4
],
minval
=
0.0
,
maxval
=
2.0
,
dtype
=
tf
.
float32
,
seed
=
1234
,
name
=
"v8_1"
))
v8_2
=
tf
.
Variable
(
tf
.
random_normal
([
2
,
3
],
mean
=
0.0
,
stddev
=
1.0
,
dtype
=
tf
.
float32
,
seed
=
1234
,
name
=
"v8_2"
))
v8_3
=
tf
.
Variable
(
tf
.
truncated_normal
([
2
,
3
],
mean
=
0.0
,
stddev
=
1.0
,
dtype
=
tf
.
float32
,
seed
=
1234
,
name
=
"v8_3"
))
v8_4
=
tf
.
Variable
(
tf
.
random_uniform
([
2
,
3
],
minval
=
0.0
,
maxval
=
1.0
,
dtype
=
tf
.
float32
,
seed
=
1234
,
name
=
"v8_4"
))
v8_5
=
tf
.
random_shuffle
([[
1
,
2
,
3
],[
4
,
5
,
6
],[
6
,
6
,
6
]],
seed
=
134
,
name
=
"v8_5"
)
# 初始化
init_op
=
tf
.
initialize_all_variables
()
# 保存变量,也可以指定保存的内容
saver
=
tf
.
train
.
Saver
()
#saver = tf.train.Saver({"my_v2": v2})
#运行
with
tf
.
Session
()
as
sess
:
sess
.
run
(
init_op
)
# 输出形状和值
print
tf
.
Variable
.
get_shape
(
v1
)
#shape
print
sess
.
run
(
v1
)
#vaule
# numpy保存文件
np
.
save
(
"v1.npy"
,
sess
.
run
(
v1
))
#numpy save v1 as file
test_a
=
np
.
load
(
"v1.npy"
)
print
test_a
[
1
,
2
]
#一些输出
print
sess
.
run
(
v3
)
v5
=
tf
.
zeros_like
(
sess
.
run
(
v1
))
print
sess
.
run
(
v6_1
)
print
sess
.
run
(
v7_1
)
print
sess
.
run
(
v8_5
)
#保存图的变量
save_path
=
saver
.
save
(
sess
,
"/tmp/model.ckpt"
)
#加载图的变量
#saver.restore(sess, "/tmp/model.ckpt")
print
"Model saved in file: "
,
save_path
|