from random import randint
from random import seed
import math
import numpy as np
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense,TimeDistributed,RepeatVector
seed(1)
def ele():
X,y = [],[]
for i in range(1):
l1=[]
for _ in range(2):
l1.append(randint(1,10))
X.append(l1)
y.append(sum(l1))
for i in range(1):
X = str(X[0][0])+'+'+str(X[0][1])
y = str(y[0])
char_to_int = dict((c, i) for i, c in enumerate(alphabet))
Xenc,yenc = [],[]
for pattern in X:
integer_encoded = [char_to_int[char] for char in pattern]
Xenc.append(integer_encoded[0])
for pattern in y:
integer_encoded = [char_to_int[char] for char in pattern]
yenc.append(integer_encoded[0])
k,k1 = [],[]
for i in range(1):
for j in Xenc:
vec = np.zeros(11)
vec[j] = 1
k.append(vec)
for j in yenc:
vec1 = np.zeros(11)
vec1[j] = 1
k1.append(vec1)
k = np.array(k)
k1 = np.array(k1)
return k,k1
alphabet = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+']
model = Sequential()
model.add(LSTM(100, input_shape=(n_in_seq_length,11)))
model.add(RepeatVector(2))
model.add(LSTM(50, return_sequences=True))
model.add(TimeDistributed(Dense(n_chars, activation='softmax')))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
for i in range(1):
X,y = ele()
#X = np.reshape(X, (4,1,11))
model.fit(X, y, epochs=1, batch_size=10)
Я получил эту ошибку:
ValueError Traceback (последний последний вызов) в () 53 X, y = ele () 54 #X = np.reshape (X, (4,1,11)) ---> 55 model.fit (X, y, epochs = 1, batch_size = 10)
~ \ Anaconda3 \ lib \ site-packages \ keras \ engine \ training.py in fit (self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, ** kwargs) 948 sample_weight = sample_weight, 949 class_weight = 949 class_weight => 950 batch_size = batch_size) 951 # Подготовить данные проверки.952 do_validation = False
~ \ Anaconda3 \ lib \ site-packages \ keras \ engine \ training.py в _standardize_user_data (self, x, y, sample_weight, class_weight, check_array_lengths, batch_size) 747 feed_input_shapes, 748 check_b_sis = 748 check_bbFalse, # Не применять размер пакета.-> 749 exception_prefix = 'input') 750 751, если y не None:
~ \ Anaconda3 \ lib \ site-packages \ keras \ engine \ training_utils.py в standardize_input_data (данные, имена, формы,check_batch_axis, exception_prefix) 125 ': ожидаемый' + names [i] + 'будет иметь' + 126 str (len (shape)) + 'размеры, но получил массив' -> 127 'с shape' + str (data_shape))128, если не check_batch_axis: 129 data_shape = data_shape [1:]
ValueError: Ошибка при проверке ввода: ожидалось, что lstm_42_input будет иметь 3 измерения, но получил массив с формой (4, 11)