new_model.layers = cross_layers AttributeError:无法设置属性

时间:2018-12-27 08:26:10

标签: python genetic-algorithm rnn

当我运行“ crossover_rnn”函数时,程序会报告错误。是因为keras版本太高了吗?

Traceback (most recent call last):
  File "E:/chauncey/ensemble-time-series-master/Ensemble.py", line 631, in <module>
    main()
  File "E:/chauncey/ensemble-time-series-master/Ensemble.py", line 621, in main
    evaluate_ga(datasets[0])
  File "E:/chauncey/ensemble-time-series-master/Ensemble.py", line 547, in evaluate_ga
    new_rnn_model = crossover_rnn(rnn_model_1,  rnn_model_2)
  File "E:/chauncey/ensemble-time-series-master/Ensemble.py", line 287, in crossover_rnn
    new_model.layers = cross_layers
  File "D:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 316, in __setattr__ 
    super(Network, self).__setattr__(name, value)
AttributeError: can't set attribute

先决条件:

Python:3.6.5

Keras:2.2.4

Tensorflow:1.5.0

“ crossover_rnn”函数使用遗传算法在交叉后生成一个隐藏层,但是在执行复制时会失败。

def crossover_rnn(model_1, model_2):
    """
    Executes crossover for the RNN in the GA for 2 models, modifying the first model
    :param model_1:
    :param model_2:
    :return:
    """
    # new_model = copy.copy(model_1)
    new_model = model_1


    ...


    # Add input layer randomly from parent 1 or parent 2
    bit_random = random.randint(0, 1)
    if bit_random == 0:
        cross_layers.insert(0, model_1.layers[0])
    else:
        cross_layers.insert(0, model_2.layers[0])

    bit_random = random.randint(0, 1)
    if bit_random == 0:
        cross_layers.append(model_1.layers[len(model_1.layers) - 1])
    else:
        cross_layers.append(model_2.layers[len(model_2.layers) - 1])

    # Set new layers
    new_model.layers = cross_layers

    return new_model

0 个答案:

没有答案