从二维数组中选一个矩形

import tensorflow as tf
data = [[1,2,3,4,5,6,7,8],[11,12,13,14,15,16,17,18]]
x = tf.strided_slice(data,[0,0],[2,4])
with tf.Session() as sess:
print(sess.run(x))

numpy array转tensor

import tensorflow as tf
import numpy as np A = list([1, 2, 3])
B = np.array([1, 2, 3])
C = tf.convert_to_tensor(A)
D = tf.convert_to_tensor(B) with tf.Session() as sess:
print(type(A))
print(type(B))
print(C.eval())
print(D.eval())

tf.train.Supervisor 用法

http://www.cnblogs.com/zhouyang209117/p/7088051.html

使用训练好的模型

import tensorflow as tf
import numpy as np
import os
log_path = r"D:\Source\model\linear"
log_name = "linear.ckpt"
# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3 # Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but TensorFlow will
# figure that out for us.)
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b # Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss) # Before starting, initialize the variables. We will 'run' this first.
saver = tf.train.Saver()
init = tf.global_variables_initializer() # Launch the graph.
sess = tf.Session()
sess.run(init) if len(os.listdir(log_path)) != 0: # 已经有模型直接读取
saver.restore(sess, os.path.join(log_path, log_name))
for step in range(201):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(W), sess.run(b))
saver.save(sess, os.path.join(log_path, log_name))

数组添加一列

import tensorflow as tf
a = [[1], [2], [3]]
b = [[1, 2, 3, 4], [1, 3, 6, 7], [4, 2, 1, 6]]
sess = tf.Session()
print(sess.run(tf.concat([a, b], 1)))

结果为:

[[1 1 2 3 4]
[2 1 3 6 7]
[3 4 2 1 6]]

一维数组合成二维数组,二维数组拆分成一维数组

import tensorflow as tf

x = tf.constant([1, 2, 3])
y = tf.constant([4, 5, 6])
z = tf.constant([7, 8, 9]) p = tf.stack([x, y, z]) sess = tf.Session()
print(sess.run(p))
print(sess.run(tf.unstack(p, num=3, axis=0)))

tf.gather从数组中选出几个元素

import tensorflow as tf
sess = tf.Session()
params = tf.constant([6, 3, 4, 1, 5, 9, 10])
indices = tf.constant([2, 0, 2, 5])
output = tf.gather(params, indices)
print(sess.run(output))
sess.close()

expand_dims

它指定维前面增加一维

# coding:utf8
import tensorflow as tf
import numpy as np
sess = tf.Session()
data = tf.constant([[1, 2, 1], [3, 1, 1]])
print(sess.run(tf.shape(data))) #(2,3)
d_1 = tf.expand_dims(data, 0) # (1,2,3)
d_1 = tf.expand_dims(d_1, 2) # (1,2,1,3)
d_1 = tf.expand_dims(d_1, -1) # (1,2,1,3,1)
d_1 = tf.expand_dims(d_1, -1) # (1,2,1,3,1,1)
print(sess.run(tf.shape(d_1)))
d_2 = d_1
print(sess.run(tf.shape(tf.squeeze(d_1))))
print(sess.run(tf.shape(tf.squeeze(d_2, [2, 4]))))

squeeze

和expand_dims的作用相反,去掉指定维,指定维的长度必须为1.

a = np.random.random((50, 1, 64))
b = tf.convert_to_tensor(a)
c = tf.squeeze(b, squeeze_dims=(1,))
with tf.Session() as sess:
print(c.eval())

split

把二维数组拆分成多个一维数组.

# coding:utf8
import tensorflow as tf
sess = tf.Session()
input = tf.random_normal([5, 30])
print(sess.run(tf.shape(input))[0] / 5)
split0, split1, split2, split3, split4 = tf.split(0, 5, input) # 二维数组变成多个一维数组
print(sess.run(tf.shape(split0)))

按第0维拆分,拆分成5个(1,30)的数组.

列出所有训练的变量

for var in tf.trainable_variables():
print("name={},shape={}".format(var.name, var.get_shape()))

最新文章

  1. 开源一个跨平台运行的服务插件 - TaskCore.MainForm
  2. idea使用maven搭建springmvc
  3. MaterialRefreshLayout
  4. MyBatis——解决字段名与实体类属性名不相同的冲突
  5. Android开发之ADT导入Support Library
  6. ApiDemos-->Views-lists-slow adapter学习
  7. Cocos2d-x 3.0 红孩儿私人义务教育 - 第一章 熟人Cocos2d-x 3.0project
  8. DOM(一)
  9. Android 自定义 permission
  10. 初识mybatis(二)
  11. P3372 【模板】线段树 1
  12. Notepad++远程连接Linux系统
  13. 使用Markdown写作
  14. python2&python3
  15. 【Android】详解Android 网络操作
  16. canvas学习之粒子动画
  17. [svc]centos6使用chkconfig治理服务和其原理
  18. 自定义Counter使用
  19. checkbox 框 选中判断
  20. 【BZOJ2288】生日礼物 [贪心]

热门文章

  1. springboot返回统一接口与统一异常处理
  2. element 动态合并表格
  3. Sturts2整合Spring报错:org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml];
  4. pat 1084 Broken Keyboard(20 分)
  5. nyoj 97-兄弟郊游问题(数学)
  6. 《算法导论中文版》PDF 下载
  7. 使用RNN进行imdb影评情感识别--use RNN to sentiment analysis
  8. webpackd学习的意义
  9. 【Luogu 3275】[SCOI2011]糖果
  10. Linux -- 进程管理之 fork() 函数