Closed Horsmann closed 3 years ago
Hello! Actually I'm facing the same problem have you solved the issue?
No, sorry. Still unsolved :(
def padWordLevel(data, max,longest_sequence):
aout=[]
for s in data:
sout = []
for w in s:
zeros = [0] * max
zeros[:-len(w)] = w
sout.append(np.array(zeros))
while len(sout) < longest_sequence:
sout.append(np.array(0))
aout.append(sout)
return np.array(aout)
change line sout.append(np.array(0))
to sout.append([])
, it works.
Maybe you should check whether the padding works as aspected.
The padding result should be an (3, 33, 10) array. but not (3, 33), with the lower level element have both vector and int.
My implement of the char seq padding function.
def padWordLevel(char_TokenInt, maxlen, longest_seq):
char_TokenIntPad = []
for char_in_token in char_TokenInt:
char_in_token = list(char_in_token)
for j in range(longest_seq-len(char_in_token)):
char_in_token.append([])
char_TokenIntPad.append(pad_sequences(char_in_token, maxlen=maxlen, padding='post'))
return np.asarray(char_TokenIntPad)
Hope it helps. And this issue may help.
Hi, I'm also trying to find a solution for that problem. As far as I understand, the embedding layer in Keras only accepts a two-dimensional array. For the character-level embeddings above, a three-dimensional array is defined (num_sentences x num_words_per_sentence x num_characters_per_word). Therefore, I get the following error when running the code above:
ValueError: Error when checking input: expected embedding_2_input to have 2 dimensions, but got array with shape (3, 33, 10)
Is there a nice way to solve this?
@mrmutator any updates?
I met the same issue, my embedding inputs have 3 dimensions, and seems keras embedding does not support this.
Is there any update?? I'm having the same issue. Keras seems to accept only 2-dimensional array!!
I changed it by using tf.nn.embedding_lookup, it can support 3-dimensional inputs.
embedding = tf.Variable(tf.random_uniform((features_total_size, n_embedding), -1, 1))
deep_inputs = tf.placeholder(tf.int32, (batch_size, time_step, features), name='deep_inputs')
embed_x = tf.nn.embedding_lookup(embedding, deep_inputs)
@Neway6655 Can you give a complete model with the shape of your data please ? I'm struggling to combine word+character embedding with Keras..
Many thanks :)
@RA-Danny I don't know how to do this using Keras, I am using tensorflow's embedding api directly.
@Neway6655 : can you please share your complete code using tensorflow ? Thanks :)
@saroufimc1 here is my code snippet, I add some comments along the code for better understanding.
brand_size = 4190 # there are 4190 brands, id from 1 to 4190.
n_embedding = 20 # I want to embedding each brand into a vector of length 20.
time_step = 12
features_cnt = 100 # one user has most 100 brands purchased history within 12 months.
batch_size = 160
with tf.name_scope('inputs'):
embedding = tf.Variable(tf.random_uniform((brand_size, n_embedding), -1, 1))
deep_inputs = tf.placeholder(tf.int32, (batch_size, time_step, features_cnt), name='deep_inputs')
print(deep_inputs.shape) # it prints (160, 12, 100)
embed_x = tf.nn.embedding_lookup(embedding, deep_inputs)
print(embed_x.shape) # it prints (160, 12, 100, 20)
deep_reshape_inputs = tf.reshape(embed_x, [-1, time_step, features_cnt * n_embedding])
with tf.name_scope('targets'):
deep_targets = tf.placeholder(tf.int32, (batch_size, 1) , name='deep_targets')
keep_prob = tf.placeholder(tf.float32, name='keep_prob')
with tf.name_scope("RNN_cells"):
purchase_cell = tf.contrib.rnn.MultiRNNCell([lstm_cell(purchase_lstm_size, keep_prob) for _ in range(num_layers)], state_is_tuple = True)
with tf.name_scope("RNN_init_state"):
purchase_initial_state = purchase_cell.zero_state(batch_size, tf.float32)
# Run the data through the RNN layers
with tf.variable_scope("RNN_purchase_forward"):
purchase_outputs, purchase_state = tf.nn.dynamic_rnn(purchase_cell, deep_reshape_inputs, initial_state=purchase_initial_state)
Wondering if the problem was solved properly. On a side-note, how did you decide the architecture of the network? Are you following a specific publication/logic/heuristic?
I found this website here: https://guillaumegenthial.github.io/sequence-tagging-with-tensorflow.html
It uses Tensorflow 1.x not Keras but the bottom line is that you run a neural network which generates LSTM outputs that you just append to the word embedding lookup table of the actual sequence tagging task you are trying to solve. Its solved with pre-processing essentially. Doing both, char and word, in a single network does seem to work with Tensorflow/Keras.
Hopefully, this helps someone..
Hi,
I have a NN in which I train a seq-2-seq model.
I want to combine word-level and character-level information. Currently I get an exception saying
setting an array element with a sequence
which probably origins in the way I provide word and char informationThe words are represented like this
For the characters I have an additional dimension e.g. one word has several characters
The modelling of the input data seems, thus, to be wrong. How would you correctly train models, one on word and one on char to concatenate them? I attached my code as minimal (not-)working example which demonstrates this error. The error messages changes if using Tensorflow but the error occurs for both frameworks.
Code: