最近在看tensorflow,顺便记录下遇到的常用函数。
2018.12.10 更新tf.eval tf.equal tf.cast tf.argmax
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 8 22:56:06 2018
@author: Administrator
"""
#可以用help("fun")查看具体的参数说明
#help(tf.argmax)axis
import tensorflow as tf
import numpy as np
#tf.equal 对比两个矩阵或者向量是否相等,相等返回True,不相等返回False,返回值是bool
#tf.cast 将X的数据格式转换成指定的格式
A = tf.constant([[1,2,3,4,5]])
B = tf.constant([[1,3,3,2,5]])
#eval 相当于tf.Session.run()
#tf.equal(A,B).eval()等同于 sess.run(tf.equal(A,B))
with tf.Session():
eval_equal=tf.equal(A,B).eval()
print("eval:",eval_equal)
with tf.Session() as sess:
equal=sess.run(tf.equal(A,B))
print(equal)
#[[ True False True False True]]
cast=sess.run(tf.cast(equal,tf.float32))
print(cast)
#[[ 1. 0. 1. 0. 1.]]
#tf.argmax 返回最大值的下标 axis=0:代表列,1:代表行
C = tf.constant([[1,2,3],[4,5,6],[5,4,3],[3,4,2]])
with tf.Session() as sess:
argmax_0=sess.run(tf.argmax(C,axis=0))
argmax_1=sess.run(tf.argmax(C,axis=1))
print(argmax_0)
#[2 1 1]
print(argmax_1)
#[2 2 0 1]
'''
1 2 3
4 5 6
5 4 3
3 4 2
'''