关键词

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}))

  

本文链接:http://task.lmcjl.com/news/12094.html

展开阅读全文