N
N
notxleb2018-09-26 20:58:33
Neural networks
notxleb, 2018-09-26 20:58:33

What's wrong with the generator?

from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import skipgrams
from keras.models import Sequential
from keras.layers import Embedding, Dense, Flatten
from keras.utils import np_utils

sentences = [line.strip() for line in open('alice_in_wonderland.txt') if line != '\n']

tokenizer = Tokenizer()
tokenizer.fit_on_texts(sentences)
corpus = tokenizer.texts_to_sequences(sentences)

V = len(tokenizer.index_word)+1
window_size = 1

model = Sequential()

model.add(Embedding(V, 64, input_length = 1))
model.add(Dense(V, activation = 'softmax'))
model.compile(loss= 'categorical_crossentropy', optimizer = 'rmsprop')

model.summary()

def generate_data(corpus, window_size, V):
    for sequence in corpus:
        idknow = skipgrams(sequence, V, window_size, negative_samples=0., shuffle= True)
        X, y = zip(*idknow[0])
        yield X, y

for epoch in range(10):
    loss = 0.
    for x, y in generate_data(corpus, window_size, V):
        print(x) 
        print(y)
        loss += model.train_on_batch(x, y)
        print(loss)

First:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================== ===========================
embedding_1 (Embedding) (None, 1, 64) 169408
_______________________________________________________________
dense_1 (Dense) (None, 1, 2647) 172055
============================================= ==================
Why is the number of parameters not the same?
Second:
Traceback (most recent call last):
File "xtest.py", line 38, in
loss += model.train_on_batch(x, y)
File "C:\Users\adels\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1209, in train_on_batch
class_weight=class_weight)
File "C:\Users\ adels\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 749, in _standardize_user_data
exception_prefix='input')
File "C:\Users\adels\AppData\Local \Programs\Python\Python36\lib\site-packages\keras\engine\training_utils.py", line 91, in standardize_input_data
data = [standardize_single_array(x) for x in data]
File "C:\Users\adels\AppData\ Local\Programs\Python\Python36\lib\site-packages\keras\engine\training_utils.py", line 91, in
data = [standardize_single_array(x) for x in data]
File "C:\Users\adels\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training_utils.py", line 26, in standardize_single_array
elif x.ndim == 1:
AttributeError: ' tuple' object has no attribute 'ndim'
What's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2018-09-26
@notxleb

Indeed, why doesn't the tuple have .ndim, and who has it ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question