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: ...
随机推荐
- 腾讯云域名申请+ssl证书申请+springboot配置https
阿里云域名申请 域名申请比较简单,使用微信注册阿里云账号并登陆,点击产品,选择域名注册 输入你想注册的域名 进入域名购买页面,搜索可用的后缀及价格,越热门的后缀(.com,.cn)越贵一般,并且很可能 ...
- js操作符类型转换
乘法 我们来看下前面的题目: console.dir(5*"5"); console.dir(5*"a");//NaN console.dir(5*NaN);/ ...
- OpenStack-Queens版 实践
OpenStack简介 OpenStack是一个由NASA(美国国家航空航天局)和Rackspace合作研发并发起的,以Apache许可证授权的自由软件和开放源代码项目. OpenStack是一个开源 ...
- java学习-java.lang.Math随机数生成
java中的随机数,java.lang.Math.random()和java.util.Random.nextDouble() Math.random()方法是生成0.0---1.0区域内的随机数,每 ...
- springcloud-06-feign的使用
在spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnection.Ap ...
- mysql 导出数据到csv文件的命令
1.导出本地数据库数据到本地文件 mysql -A service_db -h your_host -utest -ptest mysql> select * from t_apps where ...
- 自定义针对Product Key处理的TextBox
代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...
- 国际化实现之基于jquery
jQuery.i18n.properties是一款轻量级的jQuery国际化插件,能实现Web前端的国际化. jQuery.i18n.properties 采用 .properties 文件对 Jav ...
- sqlserver中利用Tran_sql把逗号分隔的字符串拆成临时表
在与数据库交互的过程中,我们经常需要把一串ID组成的字符串当作参数传给存储过程获取数据.很多时候我们希望把这个字符串转成集合以方便用于in操作. 有两种方式可以方便地把这个以某种符号分隔的ID字符串转 ...
- Ionic3与Angular4新特性
之前(17年3月底)Angular4.0.0正式发布,这个月(4月十几号)Ionic3又发布了,很多人看到这个估计都是一脸懵圈,其实,Angular4只是Angular2的后续版本,Ionic3也是I ...