質問編集履歴
1
情報の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,157 +1 @@
|
|
1
1
|
文章を学習させるときに次元数を揃えるにはどうしたらいいでしょうか?
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
今、ValueError: Error when checking input: expected input_1 to have shape (68,) but got array with shape (100,) というようにエラーが出ています。
|
6
|
-
|
7
|
-
```ここに言語を入力
|
8
|
-
|
9
|
-
・
|
10
|
-
|
11
|
-
・
|
12
|
-
|
13
|
-
def vectorize_stories(data, word_idx, 100, 100):
|
14
|
-
|
15
|
-
X = []
|
16
|
-
|
17
|
-
Xq = []
|
18
|
-
|
19
|
-
Y = []
|
20
|
-
|
21
|
-
for story, query, answer in data:
|
22
|
-
|
23
|
-
x = [word_idx[w] for w in story]
|
24
|
-
|
25
|
-
xq = [word_idx[w] for w in query]
|
26
|
-
|
27
|
-
# let's not forget that index 0 is reserved
|
28
|
-
|
29
|
-
y = np.zeros(len(word_idx) + 1)
|
30
|
-
|
31
|
-
y[word_idx[answer]] = 1
|
32
|
-
|
33
|
-
X.append(x)
|
34
|
-
|
35
|
-
Xq.append(xq)
|
36
|
-
|
37
|
-
Y.append(y)
|
38
|
-
|
39
|
-
return (pad_sequences(X, maxlen=100),
|
40
|
-
|
41
|
-
pad_sequences(Xq, maxlen=100), np.array(Y))
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
・
|
46
|
-
|
47
|
-
・
|
48
|
-
|
49
|
-
inputs_train, queries_train, answers_train = vectorize_stories(train,word_idx,100,100)
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
inputs_test, queries_test, answers_test = vectorize_stories(test,word_idx,100,100)
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
input_sequence = Input((1000,))
|
58
|
-
|
59
|
-
question = Input((1000,))
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
input_encoder_m = Sequential()
|
64
|
-
|
65
|
-
input_encoder_m.add(Embedding(input_dim=vocab_size,
|
66
|
-
|
67
|
-
output_dim=64))
|
68
|
-
|
69
|
-
input_encoder_m.add(Dropout(0.3))
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
input_encoder_c = Sequential()
|
74
|
-
|
75
|
-
input_encoder_c.add(Embedding(input_dim=vocab_size,
|
76
|
-
|
77
|
-
output_dim=1000))
|
78
|
-
|
79
|
-
input_encoder_c.add(Dropout(0.3))
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
question_encoder = Sequential()
|
84
|
-
|
85
|
-
question_encoder.add(Embedding(input_dim=vocab_size,
|
86
|
-
|
87
|
-
output_dim=64,
|
88
|
-
|
89
|
-
input_length=1000))
|
90
|
-
|
91
|
-
question_encoder.add(Dropout(0.3))
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
input_encoded_m = input_encoder_m(input_sequence)
|
96
|
-
|
97
|
-
input_encoded_c = input_encoder_c(input_sequence)
|
98
|
-
|
99
|
-
question_encoded = question_encoder(question)
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
match = dot([input_encoded_m, question_encoded], axes=(2, 2))
|
104
|
-
|
105
|
-
match = Activation('softmax')(match)
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
response = add([match, input_encoded_c])
|
110
|
-
|
111
|
-
response = Permute((2, 1))(response)
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
answer = concatenate([response, question_encoded])
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
answer = LSTM(32)(answer)
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
answer = Dropout(0.3)(answer)
|
124
|
-
|
125
|
-
answer = Dense(vocab_size)(answer)
|
126
|
-
|
127
|
-
answer = Activation('softmax')(answer)
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
model = Model([input_sequence, question], answer)
|
132
|
-
|
133
|
-
model.compile(optimizer='rmsprop', loss='categorical_crossentropy',
|
134
|
-
|
135
|
-
metrics=['accuracy'])
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
model.fit([inputs_train, queries_train], answers_train,
|
140
|
-
|
141
|
-
batch_size=32,
|
142
|
-
|
143
|
-
epochs=120,
|
144
|
-
|
145
|
-
validation_data=([inputs_test, queries_test], answers_test))
|
146
|
-
|
147
|
-
```
|
148
|
-
|
149
|
-
とコードを書きました。trainとtestには、
|
150
|
-
|
151
|
-
```ここに言語を入力
|
152
|
-
|
153
|
-
['Sandra', 'moved', 'to', 'the', 'kitchen', '.', 'John', 'travelled', 'to', 'the', 'kitchen', '.', 'Sandra', 'moved', 'to', 'the', 'hallway']
|
154
|
-
|
155
|
-
```
|
156
|
-
|
157
|
-
のようなリストに格納された文章を入れています。今回入力された値と指定した次元数が違うからこのようなエラーが出ていると思うのですが、どのように揃えたらいいのでしょうか?また、学習させる文章により長さが違いますが、その場合もどのように次元数を揃えたらいいのでしょうか?
|