[英]Model weights not changing Keras
我正在嘗試在keras中創建具有隨機權重的神經網絡。 我正在使用模型的set_weights()函數來分配隨機權重。 但是,無論權重如何,model.predict()都會在特定輸入上提供相同的輸出。 每次運行程序時,輸出都會不同,但是程序運行時輸出是相同的。 這是代碼:
ConnectFourAI.py:
from keras.models import Sequential
from keras.layers import Dense
from minimax2 import ConnectFour
import numpy as np
from time import sleep
import itertools
import random
import time
def get_model():
model = Sequential()
model.add(Dense(630, input_dim=84, kernel_initializer='uniform', activation='relu'))
model.add(Dense(630,kernel_initializer='normal', activation='relu'))
model.add(Dense(7, kernel_initializer='normal', activation='relu'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
map = {
'x':[1,0],
' ':[0,0],
'o':[0,1]
}
model = get_model()
def get_AI_move(grid):
global model
inp = np.array(list(itertools.chain.from_iterable([map[t] for t in np.array(grid).reshape(42)]))).reshape(1,84)
nnout = model.predict(inp)
# print(list(nnout[0]))
out = np.argmax(nnout)
while grid[0][out] != " ":
out = np.random.randint(7)
print("out = %d"%out)
return out
shapes = [(w.shape) for w in model.get_weights()]
print(list(model.get_weights()[0][0][0:5]))
def score_func(x, win):
if win == "x":
return 10000
elif win == " ":
return 2000
else:
return x**2
if __name__=="__main__":
for i in range(100):
weights = [np.random.randn(*s) for s in shapes]
# print(list(weights[0][0][0:5]))
model.set_weights(weights)
print(list(model.get_weights()[0][0][0:5]))
game = ConnectFour()
game.start_new()
rounds = game._round
win = game._winner
score = score_func(rounds, win)
print("%dth game scored %.3f"%(i+1,score))
seed = int(time.time()* 10**6)%(2**32)+1
np.random.seed(seed)
要重新創建此錯誤,您需要一個額外的文件。 此文件中的所有內容都可以,但是對random的唯一調用始終提供相同的值。 這是文件 。
我不知道到底出了什么問題,但是我想出了一個解決方法。 顯然,隨機模塊中存在一些問題,由於從2個不同的文件中調用隨機模塊時,會發生此行為。 因此,我使用一個文件而不是兩個文件,並得到了預期的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.