def _get_model2(self):
x1 = Input(shape=(10,))
x2 = Input(shape=(10,))
y = dot([
Dense(10)(x1),
Dense(10)(x2)
], axes=1)
model = Model(inputs=[x1, x2], outputs=y)
model.compile(loss="mse", optimizer="adam")
wrapped = OracleWrapper(model, BiasedReweightingPolicy(), score="loss")
x = [np.random.rand(16, 10), np.random.rand(16, 10)]
y = np.random.rand(16, 1)
return model, wrapped, x, y
python类dot()的实例源码
def test_tiny_cos_random(self):
np.random.seed(1988)
input_dim = 10
num_channels = 6
# Define a model
input_tensor = Input(shape = (input_dim, ))
x1 = Dense(num_channels)(input_tensor)
x2 = Dense(num_channels)(x1)
x3 = Dense(num_channels)(x1)
x4 = dot([x2, x3], axes=-1, normalize=True)
x5 = Dense(num_channels)(x4)
model = Model(inputs=[input_tensor], outputs=[x5])
# Set some random weights
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
# Get the coreml model
self._test_keras_model(model)
def getModel(input_shape):
Qfunc = Sequential()
action = Input(shape=(Player.NUM_ACTIONS,))
screen = Input(shape=input_shape)
Qfunc.add(Conv2D(32, (8, 8), strides=(4, 4),
activation='relu', padding="same",
input_shape=input_shape,
data_format="channels_first"))
Qfunc.add(Conv2D(64, (4, 4), strides=(2, 2),
activation='relu', padding="same",
data_format="channels_first"))
Qfunc.add(Conv2D(64, (4, 4), activation='relu', padding="same",
data_format="channels_first"))
Qfunc.add(MaxPooling2D(pool_size=(2, 2)))
Qfunc.add(Flatten())
Qfunc.add(Dense(128, activation='relu'))
Qfunc.add(Dense(128, activation='relu'))
Qfunc.add(Dense(Player.NUM_ACTIONS))
reward = Qfunc(screen)
model = Model(inputs=[screen, action],
outputs=dot([reward, action], axes=[1, 1]))
return Qfunc, model
def __init__(self, *args, **kwargs):
self.model = Sequential([
Dense(10, activation="relu", input_shape=(2,)),
Dense(10, activation="relu"),
Dense(2)
])
self.model.compile("sgd", "mse", metrics=["mae"])
x1 = Input(shape=(10,))
x2 = Input(shape=(10,))
y = dot([
Dense(10)(x1),
Dense(10)(x2)
], axes=1)
self.model2 = Model(inputs=[x1, x2], outputs=y)
self.model2.compile(loss="mse", optimizer="adam")
super(TestTraining, self).__init__(*args, **kwargs)
def eltwise(layer, layer_in, layerId):
out = {}
if (layer['params']['layer_type'] == 'Multiply'):
# This input reverse is to handle visualization
out[layerId] = multiply(layer_in[::-1])
elif (layer['params']['layer_type'] == 'Sum'):
out[layerId] = add(layer_in[::-1])
elif (layer['params']['layer_type'] == 'Average'):
out[layerId] = average(layer_in[::-1])
elif (layer['params']['layer_type'] == 'Dot'):
out[layerId] = dot(layer_in[::-1], -1)
else:
out[layerId] = maximum(layer_in[::-1])
return out
def skipgram_model(vocab_size, embedding_dim=100, paradigm='Functional'):
# Sequential paradigm
if paradigm == 'Sequential':
target = Sequential()
target.add(Embedding(vocab_size, embedding_dim, input_length=1))
context = Sequential()
context.add(Embedding(vocab_size, embedding_dim, input_length=1))
# merge the pivot and context models
model = Sequential()
model.add(Merge([target, context], mode='dot'))
model.add(Reshape((1,), input_shape=(1,1)))
model.add(Activation('sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy')
return model
# Functional paradigm
elif paradigm == 'Functional':
target = Input(shape=(1,), name='target')
context = Input(shape=(1,), name='context')
#print target.shape, context.shape
shared_embedding = Embedding(vocab_size, embedding_dim, input_length=1, name='shared_embedding')
embedding_target = shared_embedding(target)
embedding_context = shared_embedding(context)
#print embedding_target.shape, embedding_context.shape
merged_vector = dot([embedding_target, embedding_context], axes=-1)
reshaped_vector = Reshape((1,), input_shape=(1,1))(merged_vector)
#print merged_vector.shape
prediction = Dense(1, input_shape=(1,), activation='sigmoid')(reshaped_vector)
#print prediction.shape
model = Model(inputs=[target, context], outputs=prediction)
model.compile(optimizer='adam', loss='binary_crossentropy')
return model
else:
print('paradigm error')
return None
def create_model(data):
'''
Load keras model.
'''
# Entity branch
entity_inputs = Input(shape=(data[0].shape[1],))
entity_x = Dense(data[0].shape[1], activation='relu',
kernel_constraint=maxnorm(3))(entity_inputs)
entity_x = Dropout(0.25)(entity_x)
#entity_x = Dense(50, activation='relu',
# kernel_constraint=maxnorm(3))(entity_x)
#entity_x = Dropout(0.25)(entity_x)
# Candidate branch
candidate_inputs = Input(shape=(data[1].shape[1],))
candidate_x = Dense(data[1].shape[1], activation='relu',
kernel_constraint=maxnorm(3))(candidate_inputs)
candidate_x = Dropout(0.25)(candidate_x)
#candidate_x = Dense(50, activation='relu',
# kernel_constraint=maxnorm(3))(candidate_x)
#candidate_x = Dropout(0.25)(candidate_x)
# Cosine proximity
# cos_x = dot([entity_x, candidate_x], axes=1, normalize=False)
# cos_x = concatenate([entity_x, candidate_x])
# cos_output = Dense(1, activation='sigmoid')(cos_x)
# Match branch
match_inputs = Input(shape=(data[2].shape[1],))
match_x = Dense(data[1].shape[1], activation='relu',
kernel_constraint=maxnorm(3))(match_inputs)
match_x = Dropout(0.25)(match_x)
# Merge
x = concatenate([entity_x, candidate_x, match_x])
x = Dense(32, activation='relu', kernel_constraint=maxnorm(3))(x)
x = Dropout(0.25)(x)
x = Dense(16, activation='relu', kernel_constraint=maxnorm(3))(x)
x = Dropout(0.25)(x)
x = Dense(8, activation='relu', kernel_constraint=maxnorm(3))(x)
x = Dropout(0.25)(x)
predictions = Dense(1, activation='sigmoid')(x)
model = Model(inputs=[entity_inputs, candidate_inputs, match_inputs],
outputs=predictions)
model.compile(optimizer='RMSprop', loss='binary_crossentropy',
metrics=['accuracy'])
return model