我想写一个带有可训练参数的自定义层。实际上,我只想将此可训练对象添加到我的模型中,而无需执行任何操作。我的代码如下:
plot_map()
并且模型可以编译,但是当它运行asp = 1
过程时,会发生错误:
rgl::par3d()
好吧,我认为也许问题出在trainable_parameters from time import time
import numpy as np
import random
from keras.models import Model
import keras.backend as K
from keras.engine.topology import Layer, InputSpec
from keras.layers import Dense, Input, GaussianNoise, Layer, Activation
from keras.models import Model
from keras.optimizers import SGD, Adam
from keras.utils.vis_utils import plot_model
from keras.callbacks import EarlyStopping
class Mylayer(Layer):
def __init__(self,output_dim):
super(Mylayer,self).__init__()
self.output_dim = output_dim
def build(self,input_shape):
self.kernel = self.add_weight(shape=(input_shape[-1],self.output_dim),
initializer = 'random_uniform',
trainable=True)
def call(self,inputs):
return [inputs,self.kernel]
def compute_output_shape(self,input_shape):
return [input_shape,(input_shape[-1],self.output_dim)]
Input_1 = Input((100,))
middle = Dense(50)(Input_1)
middle = Dense(25)(middle)
middle,kernel = Mylayer(10)(middle)
output = Dense(100)(middle)
model = Model(inputs=Input_1,outputs=output)
data = np.random.randn(25,100)
model.compile(optimizer='adam',
loss='mse',
metrics=['accuracy'])
model.fit(data,
data,
batch_size=32,
epochs=5,
verbose=1)
的返回中,那么我应该如何在Keras中获得自定义可训练参数?换句话说,有什么方法可以在Keras中创建一些可训练的参数?我认为使用model.fit
将在Keras模型中造成一些混乱。
谢谢!