12 tensorflow实战:修改三维tensor矩阵的某个剖面
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 22 21:02:02 2019 @author: a
""" # -*- coding: utf-8 -*-
"""
Created on Sat Dec 1 16:53:26 2018 @author: a
"""
import tensorflow as tf
############创建三维矩阵
x = tf.placeholder(tf.int32,shape=[],name="input")
y = tf.placeholder(tf.int32,shape=[],name="input2")
z = tf.placeholder(tf.int32,shape=[],name="input3")
matrix_element_num=x*y*z
batch_sentence_nodes_vectors=tf.zeros(matrix_element_num,tf.float64)
batch_sentence_nodes_vectors=tf.reshape(batch_sentence_nodes_vectors,[x,y,z])
sess = tf.Session()
print (tf.shape(x))
xiaojie=sess.run([x,y,z],feed_dict={x:7,y:8,z:9})
print(xiaojie)
xiaojie2=sess.run(batch_sentence_nodes_vectors,feed_dict={x:7,y:8,z:9})
############创建三维矩阵
############我们目前能够做的就是,指定第一维度的值,然后将一个二维矩阵,必须小于三维矩阵的第二维度和第三维度,替换掉一整个剖面。
#def modify_one_column(tensor,columnTensor,index,numlines,numcolunms):#index也是tensor
def modify_one_profile(tensor,_2DmatrixTensor,index_firstDimension,size_firstDimension,size_secondDimension,size_thirdDimension):
##tensor为三维矩阵
##首先,我们用index_firstDimenion取出整个tensor在第一维度取值index_firstDimenion的剖面,然后分为剖面左侧部分,剖面右侧部分,然后将取出的剖面替换成二维矩阵
_2DmatrixTensor=tf.expand_dims(_2DmatrixTensor,axis=0) #扩展成为三维
new_tensor_left=tf.slice(tensor, [0,0,0], [index_firstDimension,size_secondDimension,size_thirdDimension]) #剖面左侧部分
new_tensor_right=tf.slice(tensor, [index_firstDimension+1,0,0], [size_firstDimension-index_firstDimension-1,size_secondDimension,size_thirdDimension]) #剖面右侧部分
new_tensor=tf.concat([new_tensor_left,_2DmatrixTensor,new_tensor_right],0)
return new_tensor_left,new_tensor_right,new_tensor
#下面测试将一个不够维度的二维矩阵补齐按指定维度补齐
def buqi_2DmatrixTensor(_2DmatrixTensor,lines,columns,targetlines,targetcolumns):
#首先在列上补齐
buqi_column=tf.zeros([lines,targetcolumns-columns],dtype=tf.float64)
_2DmatrixTensor=tf.concat([_2DmatrixTensor,buqi_column],axis=1)
buqi_line=tf.zeros(shape=[targetlines-lines,targetcolumns],dtype=tf.float64)
_2DmatrixTensor=tf.concat([_2DmatrixTensor,buqi_line],axis=0)
return _2DmatrixTensor
#_2DmatrixTensor=tf.ones(y*z,tf.float64)
#_2DmatrixTensor=tf.reshape(_2DmatrixTensor,[y,z])
size_firstDimension=tf.constant(7,tf.int32)
size_secondDimension=tf.constant(8,tf.int32)
size_thirdDimension=tf.constant(9,tf.int32) #_2DmatrixTensor=tf.ones(1*2,tf.float64)
#_2DmatrixTensor=tf.reshape(_2DmatrixTensor,[1,2])
#lines=tf.constant(1,tf.int32)
#columns=tf.constant(2,tf.int32)
#_2DmatrixTensor=buqi_2DmatrixTensor(_2DmatrixTensor,lines,columns,size_secondDimension,size_thirdDimension) #_2DmatrixTensor=tf.ones(8*2,tf.float64)
#_2DmatrixTensor=tf.reshape(_2DmatrixTensor,[8,2])
#lines=tf.constant(8,tf.int32)
#columns=tf.constant(2,tf.int32)
#_2DmatrixTensor=buqi_2DmatrixTensor(_2DmatrixTensor,lines,columns,size_secondDimension,size_thirdDimension) _2DmatrixTensor=tf.ones(1*9,tf.float64)
_2DmatrixTensor=tf.reshape(_2DmatrixTensor,[1,9])
lines=tf.constant(1,tf.int32)
columns=tf.constant(9,tf.int32)
_2DmatrixTensor=buqi_2DmatrixTensor(_2DmatrixTensor,lines,columns,size_secondDimension,size_thirdDimension)
## for index in range(7):
index_tensor=tf.constant(index,tf.int32)
new_tensor_left,new_tensor_right,batch_sentence_nodes_vectors=modify_one_profile(batch_sentence_nodes_vectors,_2DmatrixTensor,index_tensor,size_firstDimension,size_secondDimension,size_thirdDimension)
print (sess.run(batch_sentence_nodes_vectors,feed_dict={x:7,y:8,z:9}))
12 tensorflow实战:修改三维tensor矩阵的某个剖面的更多相关文章
- TensorFlow 实战(四)—— tensor 的认识
tensorflow,即是 tensor flows,在 computation graph 中 flows(流动)的不是别人,正是 tensor: 1. tensor 基本属性 tensor 的名字 ...
- TensorFlow实战之实现自编码器过程
关于本文说明,已同步本人另外一个博客地址位于http://blog.csdn.net/qq_37608890,详见http://blog.csdn.net/qq_37608890/article/de ...
- TensorFlow实战之实现AlexNet经典卷积神经网络
本文根据最近学习TensorFlow书籍网络文章的情况,特将一些学习心得做了总结,详情如下.如有不当之处,请各位大拿多多指点,在此谢过. 一.AlexNet模型及其基本原理阐述 1.关于AlexNet ...
- 1 如何使用pb文件保存和恢复模型进行迁移学习(学习Tensorflow 实战google深度学习框架)
学习过程是Tensorflow 实战google深度学习框架一书的第六章的迁移学习环节. 具体见我提出的问题:https://www.tensorflowers.cn/t/5314 参考https:/ ...
- Tensorflow实战(二):Discuz验证码识别
一.前言 验证码是根据随机字符生成一幅图片,然后在图片中加入干扰象素,用户必须手动填入,防止有人利用机器人自动批量注册.灌水.发垃圾广告等等 . 验证码的作用是验证用户是真人还是机器人. 本文将使用深 ...
- TensorFlow实战之Softmax Regression识别手写数字
关于本文说明,本人原博客地址位于http://blog.csdn.net/qq_37608890,本文来自笔者于2018年02月21日 23:10:04所撰写内容(http://blog.c ...
- TensorFlow 实战之实现卷积神经网络
本文根据最近学习TensorFlow书籍网络文章的情况,特将一些学习心得做了总结,详情如下.如有不当之处,请各位大拿多多指点,在此谢过. 一.相关性概念 1.卷积神经网络(ConvolutionNeu ...
- [Tensorflow实战Google深度学习框架]笔记4
本系列为Tensorflow实战Google深度学习框架知识笔记,仅为博主看书过程中觉得较为重要的知识点,简单摘要下来,内容较为零散,请见谅. 2017-11-06 [第五章] MNIST数字识别问题 ...
- 《TensorFlow实战》中AlexNet卷积神经网络的训练中
TensorFlow实战中AlexNet卷积神经网络的训练 01 出错 TypeError: as_default() missing 1 required positional argument: ...
随机推荐
- Java之集合(十八)DelayQueue
转载请注明源出处:http://www.cnblogs.com/lighten/p/7493735.html 1.前言 本章介绍阻塞队列DelayQueue,这是一个无界阻塞队列.其存储延时的元素,只 ...
- Spring Security构建Rest服务-1400-授权
安全分为 认证和授权,前边讲的都是认证,现在说授权. 前端业务系统的权限简单些,一般只区分是否登录,复杂点的还会区分 VIP用户等简单的角色,权限规则基本不变. 后台系统比较复杂,角色众多,权限随着业 ...
- require/load/include/extend的区别
require 一般用于加载一个库,当多次使用require加载一个库时,只有第一次有效,后面的都会加载失败,也就是会返回"false",以为require会追踪文件是否被加载. ...
- LogCat里的错误提示 FATAL EXCEPTION: main
程序一运行闪退. 原因为包冲突,将冲突的包删除即可.
- 9.png图片制作详细教程
1.背景自适应且不失真问题的存在 制作自适应背景图片是UI开发的一个广泛问题,也是界面设计师渴望解决的问题,我相信我们彼此都深有体会. 比如,列表的背景图一定,但是列表的高度随着列 ...
- Android 开发服务类 04_ServletForPOSTMethod
ServletForPOSTMethod 业务类 package com.wangjialin.internet.servlet; import java.io.IOException; import ...
- Impala学习–Impala后端代码分析
Table of Contents 1 代码结构 2 StateStore 3 Scheduler 4 impalad启动流程 5 Coordinator 6 ExecNode 7 PlanFragm ...
- 使用fastjson进行转换
package junit.test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; imp ...
- 1000. A-B
1000. A-B -----> http://soj.me/1000 Constraints Time Limit: 1 secs, Memory Limit: 32 MB Descripti ...
- 学习ThinkPHP笔记
学习ThinkPHP笔记 TP的模块化设计 名称 描述 应用 基于同一个入口文件访问的项目我们称之为一个应用. 模块 一个应用下面可以包含多个模块,每个模块在应用目录下面都是一个独立的子目录. 控制器 ...