1、tensorflow:模型的保存与恢复
参考:tensorflow 1.0 学习:模型的保存与恢复(Saver)
2、指定GPU设备号和个数
方法一:如果你在终端指定GPU个数和ID号,如果电脑有多个GPU,tensorflow默认全部使用。如果想只使用部分GPU,可以设置CUDA_VISIBLE_DEVICES。在调用python程序时,可以使用:
CUDA_VISIBLE_DEVICES=1 python your_script.py #在运行脚本前指定GPU的设备号
#常规设置有:
CUDA_VISIBLE_DEVICES=1 Only device 1 will be seen
CUDA_VISIBLE_DEVICES=0,1 Devices 0 and 1 will be visible
CUDA_VISIBLE_DEVICES="0,1" Same as above, quotation marks are optional
CUDA_VISIBLE_DEVICES=0,2,3 Devices 0, 2, 3 will be visible; device 1 is masked
CUDA_VISIBLE_DEVICES="" No GPU will be visible
#你也可以使用
export CUDA_VISIBLE_DEVICES=2 #指定设备号
方法二:如果你在python原文件中作更改,在在文件开始处添加如下内容:
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "2" #指明了GPU ID号
#若多个GPU的话
os.environ["CUDA_VISIBLE_DEVICES"] = "1,2" #指明了两个GPU ID号,注意这里不区分双引号和单引号
参数:
TensorFlow学习——tf.GPUOptions和tf.ConfigProto用法解析
tensorflow中使用tf.ConfigProto()配置Session运行参数&&GPU设备指定
3、tensorflow的ckpt文件总结
4、函数
tf.clip_by_value(V, min, max), 截取V使之在min和max之间
import tensorflow as tf
import numpy as np
v = tf.constant([[1.0, 2.0, 4.0],[4.0, 5.0, 6.0]])
result = tf.clip_by_value(v, 2.5, 4.5)
with tf.Session() as sess:
print(sess.run(result))
输出
[[ 2.5 2.5 4. ]
[ 4. 4.5 4.5]]
参考:tf.clip_by_value 用法
tf.pad原理和用法:参考tf.pad原理和用法
5、tf.tf.contrib.slim
tf.contrib.slim.conv2d ()
tf.contrib.slim.conv2d (inputs,
num_outputs,#[卷积核个数]
kernel_size,#[高度,宽度]
stride=1,#步长
padding='SAME',#VALID
)
参考:tf.nn.conv2d和tf.contrib.slim.conv2d的区别
def max_pool2d(inputs,
kernel_size,
stride=2,
padding='VALID',
data_format=DATA_FORMAT_NHWC,
outputs_collections=None,
scope=None):
"""Adds a 2D Max Pooling op.
Args:
inputs: 一个4-D tensor,形状为[batch_size, height, width, channels]或者[batch_size, channels, height, width]
kernel_size: 池化核的尺寸: [kernel_height, kernel_width],如果两个值相同的话可以为一个整数值。
stride: 池化步长 [stride_height, stride_width].如果两个值相同的话可以为一个整数值。
padding: 填充方式,为 'VALID' 或者 'SAME'.
data_format: 数据格式,支持 `NHWC` (default)和 `NCHW`
outputs_collections: 输出被添加到的集合
scope: 可选的name_scope.
Returns:
返回池化操作后的tensor.
6、slim.arg_scope的用法
tf.reshape
函数原型为
def reshape(tensor, shape, name=None)
第1个参数为被调整维度的张量。
第2个参数为要调整为的形状。
返回一个shape形状的新tensor
注意shape里最多有一个维度的值可以填写为-1,表示自动计算此维度。
tf.tile()用法及实例
sess.run()
Tensorflow:sess.run():参数 feed_dict等作用
tf.reduce_mean(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)
第一个参数input_tensor: 输入的待降维的tensor;
第二个参数axis: 指定的轴,如果不指定,则计算所有元素的均值;
第三个参数keep_dims:是否降维度,设置为True,输出的结果保持输入tensor的形状,设置为False,输出结果会降低维度;
第四个参数name: 操作的名称;
第五个参数 reduction_indices:在以前版本中用来指定轴,已弃用;
import tensorflow as tf
x = [[1,2,3],
[1,2,3]]
xx = tf.cast(x,tf.float32)
mean_all = tf.reduce_mean(xx, keep_dims=False)
mean_0 = tf.reduce_mean(xx, axis=0, keep_dims=False)
mean_1 = tf.reduce_mean(xx, axis=1, keep_dims=False)
with tf.Session() as sess:
m_a,m_0,m_1 = sess.run([mean_all, mean_0, mean_1])
print m_a # output: 2.0
print m_0 # output: [ 1. 2. 3.]
print m_1 #output: [ 2. 2.]
如果设置保持原来的张量的维度,keep_dims=True ,结果:
print m_a # output: [[ 2.]]
print m_0 # output: [[ 1. 2. 3.]]
print m_1 #output: [[ 2.], [ 2.]]
类似函数还有:
tf.reduce_sum :计算tensor指定轴方向上的所有元素的累加和;
tf.reduce_max : 计算tensor指定轴方向上的各个元素的最大值;
tf.reduce_all : 计算tensor指定轴方向上的各个元素的逻辑和(and运算);
tf.reduce_any: 计算tensor指定轴方向上的各个元素的逻辑或(or运算);
参考:tensorflow中 tf.reduce_mean函数
Numpy.random.seed()的用法
numpy.random.RandomState的用法
参考:numpy.random.RandomState的用法
Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
参考:成功解决Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
警告:Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
L1和L2正则化的比较?
https://blog.youkuaiyun.com/qq_26598445/article/details/82844393