根据this post,可以使用tf.shape()调整大小未知的图像,如占位符。但是该方法似乎不适用于我。我有一些简单的代码,如下所示:
import tensorflow as tf
import numpy as np
def speed_tune(x, lower_bound=0.8, upper_bound=2.0):
speed_rate = np.random.uniform(lower_bound, upper_bound)
newshape = tf.shape(x)[1:] # get the tensor shape except for rank 0(None)
newshape *= speed_rate # randomly stretch or compress the signal
return tf.resize(x, newshape)
sess = tf.InteractiveSession()
x = tf.placeholder(tf.int16, (None, 1000)) # x is a 1D audio signal
y = speed_tune(x)
data = np.random.randint(10, size=1000)
output = sess.run(y, feed_dict={x:data})
基本上,我的代码执行以下操作:给定输入1D数据x时,程序将尝试通过某种随机因子来拉伸或压缩序列,并返回已调整的序列。由于找不到任何直接执行此操作的Tensorflow函数,因此我将tf.resize视为1xD图像,其中D是信号的长度。但是我遇到了一个错误:
Traceback (most recent call last):
File "d:\SVNRepo\Python_codes\scratch.py", line 33, in <module>
y = speed_tune(x)
File "d:\SVNRepo\Python_codes\scratch.py", line 28, in speed_tune
newshape *= speed_rate # randomly stretch or compress the signal
TypeError: unsupported operand type(s) for *=: 'Tensor' and 'float'
因此,似乎tf.shape(x)
返回一个张量,而不是指定张量形状的整数值(由Tensorflow document验证)。我该怎么解决?
答案 0 :(得分:0)
不确定您要寻找的是什么,但这也许会有所帮助
使用tf.random.uniform
来避免张量/浮点运算
def speed_tune(x, lower_bound=1, upper_bound=2):
speed_rate = tf.random.uniform([1,], lower_bound, upper_bound, dtype=tf.int32)
newshape = tf.shape(x)[1:] # get the tensor shape except for rank 0(None)
newshape = newshape * speed_rate # randomly stretch or compress the signal
return tf.reshape(x, newshape)
使用过tf.reshape
,不确定tf.resize
的含义
x = tf.placeholder(tf.int32, (None, 1000)) # x is a 1D audio signal
y = speed_tune(x)
data = np.random.rand(1, 1000)
with tf.Session() as sess:
sess.run(y, feed_dict={x:data})
另一种方法是使用tf.pad
:
例如:
n = 10
tensor = tf.constant(np.random.rand(1, 10))
paddings = tf.constant([[0,1], [0,0]])
这种精确的填充设置意味着您在张量的末尾添加了n个零。为了获得初始尺寸,您需要对其进行重塑
padded = tf.pad(tensor, paddings)
output = tf.reshape(padded, [1,n*2])