我开始学习神经网络,最近我尝试创建一个文本生成器神经网络。我从其他神经网络学习网站和代码,我创建了一些功能代码。
现在我正在尝试改进神经网络,比如在输入形状中进行一个热编码(我不知道为什么我只这样做,因为我正在学习的一个网站告诉我要尝试),我在输入中有很多问题。
import numpy as np
import sys
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint
filename = "los3.txt"
raw_text = open(filename).read()
raw_text = open(filename).read()
raw_text = raw_text.lower()
#Crear mapeo de caracteres únicos a enteros, y un mapeo inverso
chars = sorted(list(set(raw_text)))
char_to_int = dict((c, i) for i, c in enumerate(chars))
#Sumarizamos los datos cargados
n_chars = len(raw_text)
n_vocab = len(chars)
print("Total caracters: ", n_chars)
print("Total vocabulario: ", n_vocab)
seq_length = 100
x_data = []
y_data = []
# loop through inputs, start at the beginning and go until we hit
# the final character we can create a sequence out of
for i in range(0, n_chars - seq_length, 1):
# Define input and output sequences
# Input is the current character plus desired sequence length
in_seq = raw_text[i:i + seq_length]
# Out sequence is the initial character plus total sequence length
out_seq = raw_text[i + seq_length]
# We now convert list of characters to integers based on
# previously and add the values to our lists
x_data.append([char_to_int[char] for char in in_seq])
x_encoded = np_utils.to_categorical(x_data)
y_data.append(char_to_int[out_seq])
n_patterns = len(x_encoded)
print ("Total Patterns:", n_patterns)
print(x_encoded.shape)
y = np_utils.to_categorical(y_data)
#shape de la matriz encoded= (3129, 100, 39)
x_encoded= x_encoded/float(n_vocab)
#Define la LSTM model
model = Sequential()
model.add(LSTM(256, input_shape=(x_encoded.shape[1], x_encoded.shape[2]), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(256))
model.add(Dropout(0.2))
model.add(LSTM(128))
model.add(Dense(y.shape[1], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
filepath = "model_weights_saved.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor="loss", verbose=1, save_best_only=True, mode="min")
desired_callbacks = [checkpoint]
model.fit(x_encoded, y, epochs=150, batch_size=256, callbacks=desired_callbacks)
然后我得到这个错误
Total caracters: 3229
Total vocabulario: 39
Total Patterns: 3129
(3129, 100, 39)
(3129, 100, 39)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [44], in <cell line: 55>()
53 #Define la LSTM model
54 model = Sequential()
---> 55 model.add(LSTM(256, input_shape=(3129, 100, 1), return_sequences=True))
56 model.add(Dropout(0.2))
57 model.add(LSTM(256))
File E:Analisis de datosanacondaenvspy37libsite-packagestensorflowpythontrainingtrackingbase.py:587, in no_automatic_dependency_tracking.<locals>._method_wrapper(self, *args, **kwargs)
585 self._self_setattr_tracking = False # pylint: disable=protected-access
586 try:
--> 587 result = method(self, *args, **kwargs)
588 finally:
589 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
File E:Analisis de datosanacondaenvspy37libsite-packageskerasutilstraceback_utils.py:67, in filter_traceback.<locals>.error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
File E:Analisis de datosanacondaenvspy37libsite-packageskerasengineinput_spec.py:214, in assert_input_compatibility(input_spec, inputs, layer_name)
212 ndim = shape.rank
213 if ndim != spec.ndim:
--> 214 raise ValueError(f'Input {input_index} of layer "{layer_name}" '
215 'is incompatible with the layer: '
216 f'expected ndim={spec.ndim}, found ndim={ndim}. '
217 f'Full shape received: {tuple(shape)}')
218 if spec.max_ndim is not None:
219 ndim = x.shape.rank
ValueError: Input 0 of layer "lstm_15" is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 3129, 100, 1)
我的数组自动从ndim=3传递到ndim=4是第一个维度=none我尝试了很多事情,但我不知道为什么我得到这个错误或如何修复输入
我认为您应该检查input_data的形状,特别是x_encoded的形状。我请求您检查一下X是否包含在内,因为在这一行中:
model.fit(X, y, epochs=150, batch_size=256, callbacks=desired_callbacks)
你已经给了X.:)可以帮助你。
在本例x_encoded = np_utils.to_categorical(x_data)
中,utils命令用于将给定的整数序列转换为二进制类矩阵。试着
tf.keras.utils.to_categorical(
y, num_classes=None, dtype='float32'
)
,您可以通过给出num_class
值来确定输出类的大小。现在,在您的输入中,它认为默认值是(largest number in input vector + 1)
这会导致x_encoded
的大小随输入向量的变化而变化。所以最好给出一个定义好的num_class值。这可能对你有帮助。:)
访问此https://www.tensorflow.org/api_docs/python/tf/keras/utils/to_categorical了解