@notxleb

Что за ошибка с генератором?

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)


Во-первых:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_1 (Embedding) (None, 1, 64) 169408
_________________________________________________________________
dense_1 (Dense) (None, 1, 2647) 172055
=================================================================

Почему кол-во параметров не одинаково?

Во-вторых:
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'

Что за ошибка?
  • Вопрос задан
  • 103 просмотра
Решения вопроса 1
longclaps
@longclaps
В самом деле, отчего у кортежа нет .ndim, и у кого он есть?
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы