相关疑难解决方法(0)

如何将Tensorflow张量尺寸(形状)作为int值?

假设我有一个Tensorflow张量.如何将张量的尺寸(形状)作为整数值?我知道有两种方法,tensor.get_shape()以及tf.shape(tensor),但我不能让形状值作为整int32数值.

例如,下面我创建了一个二维张量,我需要得到行数和列数,int32以便我可以调用reshape()以创建一个形状的张量(num_rows * num_cols, 1).但是,该方法tensor.get_shape()返回值作为Dimension类型,而不是int32.

import tensorflow as tf
import numpy as np

sess = tf.Session()    
tensor = tf.convert_to_tensor(np.array([[1001,1002,1003],[3,4,5]]), dtype=tf.float32)

sess.run(tensor)    
# array([[ 1001.,  1002.,  1003.],
#        [    3.,     4.,     5.]], dtype=float32)

tensor_shape = tensor.get_shape()    
tensor_shape
# TensorShape([Dimension(2), Dimension(3)])    
print tensor_shape    
# (2, 3)

num_rows = tensor_shape[0] # ???
num_cols = tensor_shape[1] # ???

tensor2 = tf.reshape(tensor, (num_rows*num_cols, 1))    
# Traceback (most …
Run Code Online (Sandbox Code Playgroud)

python artificial-intelligence machine-learning tensorflow

70
推荐指数
4
解决办法
9万
查看次数