简体   繁体   English

模型权重不变Keras

[英]Model weights not changing Keras

I am trying to create neural nets with random weights in keras. 我正在尝试在keras中创建具有随机权重的神经网络。 I am using set_weights() function of models, to assign random weights. 我正在使用模型的set_weights()函数来分配随机权重。 However, model.predict() gives the same output on a certain input regardless of weights. 但是,无论权重如何,model.predict()都会在特定输入上提供相同的输出。 The output differs every time I run the program, but it's same while a program is running. 每次运行程序时,输出都会不同,但是程序运行时输出是相同的。 Here is the code: 这是代码:

ConnectFourAI.py: 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)

To recreate this error, you need an extra file. 要重新创建此错误,您需要一个额外的文件。 Everything is OK in this file, but the only call to random always gives the same value. 此文件中的所有内容都可以,但是对random的唯一调用始终提供相同的值。 Here is the file . 这是文件

I don't know what exactly was going wrong, but I came up with a work around. 我不知道到底出了什么问题,但是我想出了一个解决方法。 Apparently there was some problem in random module due to which this behaviour took place when random module is called from 2 different files. 显然,随机模块中存在一些问题,由于从2个不同的文件中调用随机模块时,会发生此行为。 So I used one file instead of two, and got the results I expected. 因此,我使用一个文件而不是两个文件,并得到了预期的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM