Iterate over a tensor dimension in Tensorflow

Ángel Delgado Panadero

I am trying to develop a seq2seq model from a low level perspective (creating by myself all the tensors needed). I am trying to feed the model with a sequence of vectors as a two-dimensional tensor, however, i can't iterate over one dimension of the tensor to extract vector by vector. Does anyone know what could I do to feed a batch of vectors and later get them one by one?

This is my code:

batch_size = 100
hidden_dim = 5
input_dim = embedding_dim
time_size = 5



input_sentence = tf.placeholder(dtype=tf.float64, shape=[embedding_dim,None], name='input')
output_sentence = tf.placeholder(dtype=tf.float64, shape=[embedding_dim,None], name='output')

input_array = np.asarray(input_sentence)
output_array = np.asarray(output_sentence)

gru_layer1 = GRU(input_array, input_dim, hidden_dim) #This is a class created by myself


for i in range(input_array.shape[-1]):
    word = input_array[:,i]
    previous_state = gru_encoder.h_t
    gru_layer1.forward_pass(previous_state,word)

And this is the error that I get

TypeError: Expected binary or unicode string, got <tf.Tensor 'input_7:0' shape=(10, ?) dtype=float64>
Ángel Delgado Panadero

Finally I found an approach that solves my problem. It worked using tf.scan() instead of a loop, which doesn't require the input tensor to have a defined number in the second dimension. Consecuently you hace to prepare the input tensor previously to be parsed as you want throught tf.san(). In my case this is the code:

batch_size = 100
hidden_dim = 5
input_dim = embedding_dim
time_size = 5



input_sentence = tf.placeholder(dtype=tf.float64, shape=[embedding_dim,None], name='input')
output_sentence = tf.placeholder(dtype=tf.float64, shape=[embedding_dim,None], name='output')

input_array = np.asarray(input_sentence)
output_array = np.asarray(output_sentence)

x_t = tf.transpose(input_array, [1, 0], name='x_t')

h_0 = tf.convert_to_tensor(h_0, dtype=tf.float64)
h_t_transposed = tf.scan(forward_pass, x_t, h_0, name='h_t_transposed')
h_t = tf.transpose(h_t_transposed, [1, 0], name='h_t')

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Drop a dimension of a tensor in Tensorflow

分類Dev

Tensorflow: zip over first dimension of two tensors of different shape

分類Dev

Slicing tensor with list - TensorFlow

分類Dev

Slice tensor in Keras Tensorflow

分類Dev

Iterate over template int

分類Dev

Unable to iterate over an ElementArrayFinder

分類Dev

Creating tensorflow::Tensor from Eigen::Tensor

分類Dev

How to control dimension broadcast in tensorflow?

分類Dev

What is the shape of an image tensor in TensorFlow

分類Dev

How to check if a tensor is empty in Tensorflow

分類Dev

Operations on Specific Columns on a Tensor in TensorFlow

分類Dev

Flipping tensor and fill with zeros in tensorflow

分類Dev

PIL image _crop to tensor、in tensorflow

分類Dev

How to iterate over and filter an array?

分類Dev

iterate over unique values in PANDAS

分類Dev

iterate over GroupBy object in dask

分類Dev

how to iterate over tuple items

分類Dev

How to iterate over the lines in a string?

分類Dev

How to iterate over dates in a dataframe?

分類Dev

Iterate over a custom set in SAS

分類Dev

Dynamically iterate over static information

分類Dev

How to iterate over block of text

分類Dev

Iterate/enumerate over part of a list?

分類Dev

Iterate over each object In array

分類Dev

Jersey howto iterate over parameters?

分類Dev

input dimension reshape in Tensorflow conolutional network

分類Dev

Why use None for the batch dimension in tensorflow?

分類Dev

How to fill a tensor of values based on tensor of indices in tensorflow?

分類Dev

In Tensorflow How can I add a Tensor to another Tensor with different shape?

Related 関連記事

ホットタグ

アーカイブ