Tensorflow 机器学习项目实战 记录
来源:互联网 发布:js promise的用法 编辑:程序博客网 时间:2024/06/05 15:37
**
Tensorflow 机器学习项目实战 记录
**
基本操作
简单矩阵运算
import tensorflow as tfsess = tf.InteractiveSession()
x = tf.constant([[2, 5, 3, -5], [0, 3, -2, 5], [4, 3, 5, 3], [6, 1, 4, 0]])y = tf.constant([[4, -7, 4, -3, 4], [6, 4, -7, 4, 7], [2, 3, 2, 1, 4], [1, 5, 5, 5, 2]])floatx = tf.constant([[2., 5., 3., -5.], [0., 3., -2., 5.], [4., 3., 5., 3.], [6., 1., 4., 0.]])
# 矩阵转置tf.transpose(x).eval() array([[ 2, 0, 4, 6], [ 5, 3, 3, 1], [ 3, -2, 5, 4], [-5, 5, 3, 0]])
#矩阵相乘 Matrix Multiplicationtf.matmul(x, y).eval()array([[ 2, 0, 4, 6], [ 5, 3, 3, 1], [ 3, -2, 5, 4], [-5, 5, 3, 0]])
#矩阵行列式(determinant)tf.matrix_determinant(floatx).eval()817.99969
#逆矩阵(inverse matrix)tf.matrix_inverse(floatx).eval()array([[-0.00855745, 0.10513447, -0.18948655, 0.29584354], [ 0.12958434, 0.12224938, 0.01222495, -0.05134475], [-0.01955992, -0.18826404, 0.28117359, -0.18092909], [-0.08557458, 0.05134474, 0.10513448, -0.0415648 ]], dtype=float32)
#行列式求解tf.matrix_solve(floatx, [[1],[1],[1],[1]]).eval()array([[ 0.202934 ], [ 0.21271393], [-0.10757945], [ 0.02933985]], dtype=float32)
**
约简(reduction)
**
x = tf.constant([[1, 2, 3], [3, 2, 1], [-1, -2, -3]])boolean_tensor = tf.constant([[True, False, True], [False, False, True], [True, False, False]])
# 乘积方式降维,reduction_indices=1在行内计算,reduction_indices=0在列内计算tf.reduce_prod(x, reduction_indices= 1).eval()array([ 6, 6, -6])
tf.reduce_prod(x, reduction_indices= 0).eval(0)Out[15]:array([-3, -8, -9])
# 最小值降维tf.reduce_min(x, reduction_indices=1).eval()array([ 1, 1, -3])
#最大值降维tf.reduce_max(x, reduction_indices=1).eval()array([ 3, 3, -1])
#平均值tf.reduce_mean(x, reduction_indices=1).eval()array([ 2, 2, -2])
# 全部为真,则为真tf.reduce_all(boolean_tensor,reduction_indices=1).eval()array([False, False, False], dtype=bool)
# 存在真,则为真tf.reduce_any(boolean_tensor, reduction_indices=1).eval()array([ True, True, True], dtype=bool)
张量分解
seg_ids = tf.constant([0, 1, 1, 2, 2]) # 里面的数字代表分解后所在的index位置。分组的索引: 0 | 1,2 | 3, 4tens1 = tf.constant([[2, 5, 3, -5], [0, 3, -2, 5], [4, 3, 5, 3], [6, 1, 4, 0], [6, 1, 4, 0]])
#0组求和作为新的0组 1,2组求和作为新的第1组 3,4组求和作为新的第2组tf.segment_sum(tens1,segment_ids=seg_ids).eval() # sum segmentationarray([[ 2, 5, 3, -5], [ 4, 6, 3, 8], [12, 2, 8, 0]])
tf.segment_prod(tens1, segment_ids=seg_ids).eval()array([[ 2, 5, 3, -5], [ 0, 9, -10, 15], [ 36, 1, 16, 0]])
还有tf.segment_max,tf.segment_min,tf.segment_mean等
序列
x = tf.constant([[2, 5, 3, -5], [0, 3, -2, 5], [4, 3, 5, 3], [6, 1, 4, 0]])listx = tf.constant([1,2,3,4,5,6,7,8])listy = tf.constant([4,5,8,9])boolx = tf.constant([[True, False], [False, True]]) # 返回x中最大元素的坐标tf.argmax(x,axis=1).eval()array([1, 3, 2, 0], dtype=int64)
#同理tf.argmin(x,axis=1).eval()array([3, 2, 1, 3], dtype=int64)#tf.listdiff 被移除#返回真值坐标tf.where(boolx).eval()array([[0, 0], [1, 1]], dtype=int64)
#返回listx中不重复包含的数,并且返回这些数的下标indexitem, index = tf.unique(listx)print(item.eval())print(index.eval())[1 2 3 4 5 6 7 8][0 1 2 3 4 5 6 7]
张量形状变换
x = tf.constant([[2, 5, 3, -5], [0, 3, -2, 5], [4, 3, 5, 3], [6, 1, 4, 0]])tf.shape(x).eval() array([4, 4])
# tensor大小tf.size(x).eval()16#tensor的rank(秩) 等于维度tf.rank(x).eval()2# -1代表视其他维度而定tf.reshape(x , [-1, 2]).eval()array([[ 2, 5], [ 3, -5], [ 0, 3], [-2, 5], [ 4, 3], [ 5, 3], [ 6, 1], [ 4, 0]])
y = tf.reshape(x, [-1, 1])print(tf.shape(y).eval())y.eval()[16 1]Out[66]:array([[ 2], [ 5], [ 3], [-5], [ 0], [ 3], [-2], [ 5], [ 4], [ 3], [ 5], [ 3], [ 6], [ 1], [ 4], [ 0]]) """Given a tensor `input`, this operation returns a tensor of the same type with all dimensions of size 1 removed. If you don't want to remove all size 1 dimensions, you can remove specific size 1 dimensions by specifying"""# 维度大小为1的都被移除z = tf.squeeze(y)z.shape(16,)zarray([ 2, 5, 3, -5, 0, 3, -2, 5, 4, 3, 5, 3, 6, 1, 4, 0])
```python # 't' is a tensor of shape [2] tf.shape(tf.expand_dims(t, 0)) # [1, 2] tf.shape(tf.expand_dims(t, 1)) # [2, 1] tf.shape(tf.expand_dims(t, -1)) # [2, 1] # 't2' is a tensor of shape [2, 3, 5] tf.shape(tf.expand_dims(t2, 0)) # [1, 2, 3, 5] tf.shape(tf.expand_dims(t2, 2)) # [2, 3, 1, 5] tf.shape(tf.expand_dims(t2, 3)) # [2, 3, 5, 1] ```# 在指定的axis上扩展维度tf.expand_dims(x, 1).eval()array([[[ 2, 5, 3, -5]], [[ 0, 3, -2, 5]], [[ 4, 3, 5, 3]], [[ 6, 1, 4, 0]]])
阅读全文
0 0
- Tensorflow 机器学习项目实战 记录
- Tensorflow实战学习(八)【机器学习基础 线性回归】
- Python开源机器学习项目实战
- 机器学习项目开发实战,应用
- TensorFlow学习笔记6——《面向机器智能的TensorFlow实践》StanfordDog例程修改记录
- 机器学习6-tensorflow
- 新手机器学习-tensorflow
- 机器学习-TensorFlow安装
- 记录每天学习机器学习的经历,对照机器学习实战这本书
- Tensorflow实战学习(四十三)【TF.Learn 机器学习Estimator,DataFrame,监督器Monitors】
- 《机器学习实战》学习
- tensorflow(二)学习记录
- TensorFlow学习记录[1]
- Tensorflow学习记录
- 【机器学习】Tensorflow学习笔记
- 机器学习项目实战之贝叶斯垃圾邮件分类
- 机器学习项目实战:泰坦尼克号获救预测
- 机器学习项目实战之贷款申请最大利润
- mysql windows环境安装使用
- GStreamer建议的学习步骤和网页链接汇总
- Android高德地图 实现定位 周边热点 POI搜索 BottomSheetBehavior 动态获取权限demo
- 消除光标
- SparkStreaming之foreachRDD
- Tensorflow 机器学习项目实战 记录
- 12月7日云栖精选夜读:特鲁多对话马云:请为加拿大小企业多花一些时间!
- 带你测试对比深度学习框架!TensorFlow,Keras,PyTorch...哪家强?(附数据集)
- Django系列教程(4)-- 模型中的F对象和Q对象
- 关于百度地图api的使用心得
- C语言的第七天学习
- mysql 远程访问
- OverlayIcon 制作流程及注意项
- c#中DateTimeKind的