所以我有一个形状h_in
的张量(50, ?, 1, 100)
我现在想通过在轴(50, 1, 1, 100)
上取最大值来变成1
形状。
我该怎么做?
我试过
h_out = max_pool(h_in)
与
def max_pool(h,ksize=[1,-1,1,1],strides=[1,1,1,1],padding='VALID'):
return tf.nn.max_pool(h,ksize=ksize,strides=strides,padding=padding)
但这似乎并没有减小尺寸。
可运行的例子:
import tensorflow as tf
import numpy as np
import numpy.random as nprand
def _weight_variable(shape,name):
initial = tf.truncated_normal(shape,stddev=0.1)
v = tf.Variable(initial,name=name)
return v
def _bias_variable(shape,name):
initial = tf.constant(0.1,shape=shape)
v = tf.Variable(initial,name=name)
return v
def _embedding_variable(shape,name):
initial = tf.truncated_normal(shape)
v = tf.Variable(initial,name=name)
return v
def conv2d(x,W,strides=[1,1,1,1],padding='VALID'):
return tf.nn.conv2d(x,W,strides=strides,padding=padding)
def max_pool(h,ksize=[1,-1,1,1],strides=[1,1,1,1],padding='VALID'):
return tf.nn.max_pool(h,ksize=ksize,strides=strides,padding=padding)
nof_embeddings= 55000
dim_embeddings = 300
batch_size = 50
filter_size = 100
x_input = tf.placeholder(tf.int32, shape=[batch_size, None])
def _model():
embeddings = _embedding_variable([nof_embeddings,dim_embeddings],'embeddings')
h_lookup = tf.nn.embedding_lookup(embeddings,x_input)
h_embed = tf.reshape(h_lookup,[batch_size,-1,dim_embeddings,1])
f = 3
W_conv1f = _weight_variable([f,dim_embeddings,1,filter_size],f'W_conv1_{f}')
b_conv1f = _bias_variable([filter_size],f'b_conv1_{f}')
h_conv1f = tf.nn.relu(conv2d(h_embed,W_conv1f) + b_conv1f)
h_pool1f = max_pool(h_conv1f)
print("h_embed:",h_embed.get_shape())
print()
print(f'h_conv1_{f}:',h_conv1f.get_shape())
print(f'h_pool1_{f}:',h_pool1f.get_shape())
print()
return tf.shape(h_pool1f)
if __name__ == '__main__':
tensor_length = 35
model = _model()
with tf.Session() as sess:
tf.global_variables_initializer().run()
batch = nprand.randint(0,nof_embeddings,size=[batch_size,tensor_length])
shape = sess.run(model,
feed_dict ={
x_input : batch
})
print('result:',shape)
输出
h_embed: (50, ?, 300, 1)
h_conv1_3: (50, ?, 1, 100)
h_pool1_3: (50, ?, 1, 100)
result: [ 50 35 1 100]
让我说我改为硬编码我想要的尺寸:
h_pool1f = max_pool(h_conv1f,ksize=[1,35-f+1,1,1])
有效。
但是,一旦我更改tensor_length
(这是在运行时确定的,所以不,我不能对其进行硬编码),我现在遇到了麻烦。
一个"解决方案"可能是通过填充或其他东西将输入吹到固定的最大长度,但是再次,这会引入不必要的计算和人工上限,我应该非常希望避免这两种情况。
那么,有吗
-1
中的k_size
?答案 0 :(得分:3)
我认为tf.reduce_max
正是您所寻找的:
https://www.tensorflow.org/api_docs/python/tf/reduce_max
用法:
tens = some tensorflow.Tensor
ax = some positive integer, or -1 or None
red_m = tf.reduce_max(tens, axis=ax)
如果数字为[shape_0, shape_1, shape_2]
,则结果张量red_m
的形状[shape_1, shape_2]
如果ax=0
,形状[shape_0, shape_2]
ax=1
,等等。如果ax=-1
,则推断出最后的轴,而如果ax=None
,则减少将沿所有轴发生。