1.os.listdir()
用于返回指定的文件夹包含的文件或文件夹的名字的列表, 它不包括 '.' 和'..' 即使它在文件夹中。
import os
# 打开文件
path = "C:/Users/ch/Desktop/my/log/"
dirs = os.listdir(path)
# 输出所有文件和文件夹
for file in dirs:
print(file)
结果:
model.ckpt-0.data-00000-of-00001
model.ckpt-0.index
model.ckpt-0.meta
model.ckpt-200.data-00000-of-00001
model.ckpt-200.index
model.ckpt-200.meta
model.ckpt-400.data-00000-of-00001
model.ckpt-400.index
model.ckpt-400.meta
model.ckpt-600.data-00000-of-00001
model.ckpt-600.index
model.ckpt-600.meta
model.ckpt-800.data-00000-of-00001
model.ckpt-800.index
model.ckpt-800.meta
model.ckpt-999.data-00000-of-00001
model.ckpt-999.index
model.ckpt-999.meta
进程已结束,退出代码0
2.tf.nn.top_k(input, k, name=None)
这个函数的作用是返回 input 中每行最大的 k 个数,并且返回它们所在位置的索引。
#例1
import tensorflow as tf
import numpy as np
input = tf.constant(np.random.rand(3, 4))
k = 2
output = tf.nn.top_k(input, k)
with tf.Session() as sess:
print(sess.run(input))
print(sess.run(output))
结果:
[[0.11749906 0.71289315 0.47632536 0.90901044]
[0.14741858 0.00648505 0.10690213 0.50449713]
[0.419041 0.27644893 0.6627564 0.56422511]]
TopKV2(values=array([[0.90901044, 0.71289315],
[0.50449713, 0.14741858],
[0.6627564 , 0.56422511]]), indices=array([[3, 1],
[3, 0],
[2, 3]]))
进程已结束,退出代码0
3.tf.nn.in_top_k(predictions, targets, k, name=None)
这个函数的作用是返回一个布尔向量,说明目标值是否存在于预测值之中。
注意:targets 是predictions中的索引位,并不是 predictions 中具体的值。
#例2
import tensorflow as tf
import numpy as np
input = tf.constant(np.random.rand(3, 4), tf.float32)
k = 1 # targets对应的索引是否在最大的前k(2)个数据中
output = tf.nn.in_top_k(input, [3, 3, 3], 1)
with tf.Session() as sess:
print(sess.run(input))
print(sess.run(output))
结果:
对于这几个函数的解释,要感谢https://blog.csdn.net/wuguangbin1230/article/details/728206
4.tf.argmax()
tf.argmax(input,axis)根据axis取值的不同返回每行或者每列最大值的索引。
axis = 0:
axis=0时比较每一列的元素,将每一列最大元素所在的索引记录下来,最后输出每一列最大元素所在的索引数组。
import numpy as np
test = np.array([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]])
print(np.argmax(test, 0))#每列最大值的索引
axis = 1:
axis=1的时候,将每一行最大元素所在的索引记录下来,最后返回每一行最大元素所在的索引数组。
5.tf.equal()
import tensorflow as tf
A = [[1, 3, 4, 5, 6]]
B = [[1, 3, 4, 3, 2]]
with tf.Session() as sess:
print(sess.run(tf.equal(A, B)))
结果:[[ True True True False False]]
6.tf.reduce_mean()
tf.reduce_mean 函数用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值。
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:在以前版本中用来指定轴,已弃用;
结果:2.0 [1. 2. 3.] [2. 2.]