2 回答

TA貢獻1853條經(jīng)驗 獲得超6個贊
我相信解決方案如下:
import keras
from keras import backend as K
from keras.models import Sequential, Model
from keras.layers import Input, LSTM, RepeatVector, TimeDistributed
from keras.layers.core import Flatten, Dense, Dropout, Lambda
from keras.optimizers import SGD, RMSprop, Adam
from keras import objectives
from keras.engine.topology import Layer
import numpy as np
class LayerKMultiply(Layer):
def __init__(self, output_dim, **kwargs):
self.output_dim = output_dim
self.k = None
super(LayerKMultiply, self).__init__(**kwargs)
def build(self, input_shape):
# Create a trainable weight variable for this layer.
self.k = self.add_weight(
name='k',
shape=(),
initializer='ones',
dtype='float32',
trainable=True,
)
super(LayerKMultiply, self).build(input_shape) # Be sure to call this at the end
def call(self, x):
return self.k * x
def compute_output_shape(self, input_shape):
return (input_shape[0], input_shape[1], input_shape[2])
添加回答
舉報