[英]Keras ValueError: Dimensions must be equal, but are 6 and 9 for '{{node Equal}}
這是錯誤
ValueError: Dimensions must be equal, but are 6 and 9 for '{{node Equal}} = Equal[T=DT_FLOAT, incompatible_shape_error=true](IteratorGetNext:1, Cast_1)' with input shapes: [?,6], [?,9]
I'm trying to give a simple Keras network a group of 9 by 3 numpy arrays of integers with an intended output of a softmax on 6 categories, with a target being a one hot categorization on 6 categories. 我正在使用填充來創建一致的 9,3 arrays(我很想擺脫它,但這會產生大量其他錯誤)。 現在,model.fit 接受輸入和目標,但在運行時遇到此錯誤。 我相信它試圖將 9 x 3 輸入數組的每一行與目標數組的每個內部元素相關聯。 這不是我想要建立的預期關聯。 我想獲取整個 9 x 3 數組並將其與 6 個類別之一相關聯。 顯然,我做錯了什么,但我不知道它是什么。 我四處尋找有類似問題的人,但找不到具有{{node Equal}}
部分的人。 大多數具有相同 ValueError 的其他人在那里有一個不同的部分,這使得它看起來無關緊要,包括一些在 Kera 中直接出現的錯誤。
這是一些重新創建錯誤的簡單代碼。
import tensorflow as tf
import numpy as np
from tensorflow import keras
model = keras.Sequential()
model.add(keras.layers.Dense(16, input_shape=(9, 3), activation='relu'))
model.add(keras.layers.Dense(32, activation='relu'))
model.add(keras.layers.Dense(32, activation='relu'))
model.add(keras.layers.Dense(16, activation='relu'))
model.add(keras.layers.Dense(6, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
example_array = np.array([
np.array([[ 5, 0, 1],
[ 2, 0, 1],
[ 4, 0, 1],
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1]]),
np.array([[ 4, 3, 0],
[ 4, 2, 2],
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1]]),
np.array([[ 3, 0, 2],
[ 1, 1, 1],
[ 3, 2, 0],
[ 3, 0, 3],
[ 1, 0, 2],
[ 4, 1, 1],
[ 1, 1, 1],
[ 3, 1, 1],
[-1, -1, -1]])])
example_target = np.array([[1, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 1, 0,],
[0, 0, 0, 0, 0, 1,]])
model.fit(example_array, example_target, epochs=1)
盡管這似乎會產生一個與 (Cast_2, Cast_3) 位與原始 (IteratorGetNext:1, Cast_1) 位不同的錯誤
ValueError: Dimensions must be equal, but are 6 and 9 for '{{node Equal}} = Equal[T=DT_FLOAT, incompatible_shape_error=true](Cast_2, Cast_3)' with input shapes: [?,6], [?,9].
考慮到我從我的主代碼的示例運行中獲取了這個例子,這不應該發生,但是如果你想與之交互,這里是我的主代碼。
Network.py
import gym
import random
import numpy as np
import tensorflow as tf
from tensorflow import keras
from statistics import median, mean
from collections import Counter
import Mastermind
INITIAL_GAMES = 1000
#The number of avaliable colours
COLOURS = 6
GUESS_LENGTH = 4
#The number of guesses avaliable to the player
GUESSES = 10
env = Mastermind.MastermindEnv(GUESS_LENGTH, COLOURS, GUESSES)
colours = env.colours
def initial_games():
# [OBS, MOVES]
training_data = []
# all scores:
scores = []
# just the scores that met our threshold:
accepted_scores = []
# iterate through however many games we want:
for _ in range(INITIAL_GAMES):
env.reset()
score = 0
# guesses and results specifically from this environment:
game_memory = []
# Each is given the number of guesses
# long plus one to garuentee no interrupts
for t in range(GUESSES+1):
# choose random guess
guess = ""
for _ in range(GUESS_LENGTH):
guess += random.choice(colours)
#Check guess
observation, reward, done, info = env.step(guess)
score += reward
if done:
#Memory is saved after game's completion
game_memory = observation
break
# If our score is positive it means that the answer was
# correctly guessed at which point we want to save the score
# and the game memory
if 10 > score > 0:
accepted_scores.append(score)
training_data.append(game_memory)
# reset env to play again
env.reset()
# save overall scores
scores.append(score)
# just in case you wanted to reference later
training_data_save = np.array(training_data)
np.save('saved.npy',training_data_save)
#some statistics stuff
print('Average accepted score:',mean(accepted_scores))
print('total games won:', len(accepted_scores))
print(Counter(accepted_scores))
return training_data
model = keras.Sequential()
model.add(keras.layers.Dense(16, input_shape=(9, 3), activation='relu'))
model.add(keras.layers.Dense(32, activation='relu'))
model.add(keras.layers.Dense(32, activation='relu'))
model.add(keras.layers.Dense(16, activation='relu'))
model.add(keras.layers.Dense(COLOURS, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
def initial_train_model():
training_data = initial_games()
for index in range(GUESS_LENGTH):
training = []
targets = []
for observation in training_data:
obs = []
for i in range(len(observation)):
if i == len(observation)-1:
targets.append(ord(observation[i][0][index])-97)
else:
obs.append([ord(observation[i][0][index])-97,
ord(observation[i][1])-48,
ord(observation[i][2])-48])
i += 1
j = 10-len(observation)
while j > 0:
obs.append([-1, -1, -1])
j -= 1
training.append(np.array(obs))
print(training)
training = np.array(training)
print(keras.utils.to_categorical(targets, num_classes=COLOURS))
one_hot_targets = np.array(keras.utils.to_categorical(targets, num_classes=COLOURS))
#print(one_hot_targets)
model.fit(training, one_hot_targets, epochs=1)
initial_train_model()
和
Mastermind.py
import gym
from gym.utils import seeding
import numpy as np
import random
class MastermindEnv(gym.Env):
"""
Description:
A code guessing board game where a series of letters must be guessed
which gives small pieces of information to the player.
Observation:
Type: List of Lists
Guess Blacks Whites
String int int
Actions:
Type: Discrete(colours^guess_length)
String guess
A string of length guess_length where each character can be any colour.
Reward:
Reward is 10 on game completion with 1 additional
for each remaining guess.
Starting State:
An empty board with a target created
Episode Termination:
Remaining guesses reduced to 0
guess == target
Solved Requirements:
100% winrate over 20 games
"""
"""
metadata = {
'render.modes': ['human', 'rgb_array'],
'video.frames_per_second' : 50
}
"""
def __init__(self, guess_length=4, colours=6, guesses=10):
self.guess_length = guess_length
self.total_guesses = guesses
self.guesses = guesses
self.colours = []
for _ in range(colours):
self.colours.append(chr(_ + 97))
self.seed()
self.state = []
def seed(self, seed=None):
self.np_random, seed = seeding.np_random(seed)
return [seed]
def step(self, guess):
done = False
correct = False
self.guesses -= 1
if guess == self.target:
done = True
correct = True
blacks = 0
whites = 0
accounted = []
for i in range(len(guess)):
if guess[i] == self.target[i]:
blacks += 1
accounted.append(i)
continue
for j in range(len(self.target)):
if i != j and j not in accounted:
if guess[i] == self.target[j]:
whites += 1
accounted.append(j)
break
self.state.append([guess, blacks, whites])
if self.guesses == 0:
done = True
if not done:
reward = 0.0
else:
if correct:
reward = float(self.guesses+1)
else:
reward = 0.0
return np.array(self.state), reward, done, {}
def reset(self):
self.state = []
self.guesses = self.total_guesses
# Creating a target
target = ""
for _ in range(self.guess_length):
target += random.choice(self.colours)
#print(target)
self.target = target
return np.array(self.state)
def render(self, mode='human'):
print(self.state[-1])
def close(self):
self.state = []
self.guesses = self.total_guesses
主要是,如果可能,我希望每個 (9, 3) 數組與目標 arrays 的單行相關聯。
謝謝你。
問題可能出在 model 的定義中。 您的輸入數據有太多維度(4 個維度),無法直接適合Dense
層(輸入為 1 個維度,輸出為 1 個維度)。 您應該在第一個Dense
圖層之前添加一個Flatten
圖層。 在您的情況下,您不再需要任何Flatten
層,因為Dense
層的 output 具有與其輸入相同的尺寸。
除此之外,您還應該將input_shape
的 input_shape 定義為input_shape=(9, 3, 3)
,因為您的 model 由 3 個 ZA3CBC3F9D0CE2F2C1554E1B671D71D9 組成
因此,您的示例代碼應該是這樣的:
import tensorflow as tf
import numpy as np
from tensorflow import keras
model = keras.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(keras.layers.Dense(16, input_shape=(9, 3, 3), activation='relu'))
model.add(keras.layers.Dense(32, activation='relu'))
model.add(keras.layers.Dense(32, activation='relu'))
model.add(keras.layers.Dense(16, activation='relu'))
model.add(keras.layers.Dense(6, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
example_array = np.array([
np.array([[ 5, 0, 1],
[ 2, 0, 1],
[ 4, 0, 1],
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1]]),
np.array([[ 4, 3, 0],
[ 4, 2, 2],
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1],
[-1, -1, -1]]),
np.array([[ 3, 0, 2],
[ 1, 1, 1],
[ 3, 2, 0],
[ 3, 0, 3],
[ 1, 0, 2],
[ 4, 1, 1],
[ 1, 1, 1],
[ 3, 1, 1],
[-1, -1, -1]])])
example_target = np.array([[1, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 1, 0,],
[0, 0, 0, 0, 0, 1,]])
model.fit(example_array, example_target, epochs=1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.