考虑一下“猫在楼上”这句话的3克,每个单词都用@和~符号隔开。
trigrams = ['@th', 'the', 'he~', '@ca', 'cat', 'at~', '@is', 'is~',
'@up', 'ups', 'pst', 'sta', 'tai', 'air', 'irs', 'rs~']我想用这个句子训练一个基于字符的前馈神经语言模型,但是我很难正确地拟合X和y参数。
我的代码如下:
# trigrams encoded
d = dict([(y,x+1) for x,y in enumerate(sorted(set(trigrams)))])
trigrams_encoded = [d[x] for x in trigrams]
# trigrams_encoded = [3, 15, 8, 1, 7, 6, 2, 10, 4, 16, 11, 13, 14, 5, 9, 12]
# x_train
x_train = [] # list of lists, each list contains 3 encoded trigrams
for i in range(len(trigrams_encoded)-3) :
lst = trigrams_encoded[i:i+3]
x_train.append(lst)
x_train = np.array(x_train) # x_train shape is (13,3)
# y_train
y_train = trigrams_encoded[3:]
data = np.array(y_train)
y_onehot = to_categorical(data) # y_onehot shape is (13,17)
y_onehot = np.delete(y_onehot, 0, 1) # now shape is (13,16)
# define model
model = Sequential()
model.add(Embedding(len(d), 10, input_length=3)) #len(d) = 16
model.add(Flatten())
model.add(Dense(10, activation='relu'))
model.add(Dense(len(d), activation='softmax'))
# compile the model
# i have set sparse_categorical_crossentropy here, but not sure if this is correct. feel free to change it
model.compile(loss="sparse_categorical_crossentropy", optimizer='adam', metrics=['accuracy'])
# train the model
model.fit(x_train, y_onehot, epochs=1, verbose=0)我最初的尝试是说,自从input_length=3,模型将作为输入三重奏列出的n-克,应该被标记为下一个n克在列表中。但这似乎失败了。(应该失败吗?)
上面的代码引发以下错误,我不知道如何解决:
"InvalidArgumentError: Graph execution error:
Detected at node 'sequential/embedding/embedding_lookup' defined at (most recent call last):
(... many lines...)
Node: 'sequential/embedding/embedding_lookup'
indices[5,1] = 16 is not in [0, 16)"你能在这里协助正确选择X和Y吗?
发布于 2022-04-28 10:24:26
使用categorical_crossentropy作为丢失函数时,代码运行良好,因为您使用的是一个热编码标签:
import numpy as np
import tensorflow as tf
trigrams = ['@th', 'the', 'he~', '@ca', 'cat', 'at~', '@is', 'is~',
'@up', 'ups', 'pst', 'sta', 'tai', 'air', 'irs', 'rs~']
# trigrams encoded
d = dict([(y,x+1) for x,y in enumerate(sorted(set(trigrams)))])
trigrams_encoded = [d[x] for x in trigrams]
# trigrams_encoded = [3, 15, 8, 1, 7, 6, 2, 10, 4, 16, 11, 13, 14, 5, 9, 12]
# x_train
x_train = [] # list of lists, each list contains 3 encoded trigrams
for i in range(len(trigrams_encoded)-3) :
lst = trigrams_encoded[i:i+3]
x_train.append(lst)
x_train = np.array(x_train) # x_train shape is (13,3)
# y_train
y_train = trigrams_encoded[3:]
data = np.array(y_train)
y_onehot = tf.keras.utils.to_categorical(data) # y_onehot shape is (13,17)
y_onehot = np.delete(y_onehot, 0, 1) # now shape is (13,16)
# define model
model = tf.keras.Sequential()
model.add(tf.keras.layers.Embedding(len(d) + 1, 10, input_length=3)) #len(d) = 16
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(10, activation='relu'))
model.add(tf.keras.layers.Dense(len(d), activation='softmax'))
model.compile(loss="categorical_crossentropy", optimizer='adam', metrics=['accuracy'])
# train the model
model.fit(x_train, y_onehot, epochs=5, verbose=1)sparse_categorical_crossentropy只适用于稀疏整数值。
https://stackoverflow.com/questions/72032858
复制相似问题