Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added bettter reset() function to sequential model #2079

Closed
wants to merge 11 commits into from
51 changes: 43 additions & 8 deletions keras/layers/convolutional.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import

from .. import backend as K
from .. import activations, initializations, regularizers, constraints
from ..layers.core import Layer
Expand Down Expand Up @@ -74,6 +73,7 @@ def __init__(self, nb_filter, filter_length,
W_regularizer=None, b_regularizer=None, activity_regularizer=None,
W_constraint=None, b_constraint=None,
input_dim=None, input_length=None, **kwargs):
self.resettable = True

if border_mode not in {'valid', 'same'}:
raise Exception('Invalid border mode for Convolution1D:', border_mode)
Expand Down Expand Up @@ -106,8 +106,9 @@ def __init__(self, nb_filter, filter_length,
def build(self):
input_dim = self.input_shape[2]
self.W_shape = (self.nb_filter, input_dim, self.filter_length, 1)
self.W = self.init(self.W_shape, name='{}_W'.format(self.name))
self.b = K.zeros((self.nb_filter,), name='{}_b'.format(self.name))

(self.W, self.b) = self.init_weights()

self.trainable_weights = [self.W, self.b]
self.regularizers = []

Expand All @@ -127,6 +128,16 @@ def build(self):
self.set_weights(self.initial_weights)
del self.initial_weights

def init_weights(self):
W = self.init(self.W_shape, name='{}_W'.format(self.name))
b = K.zeros((self.nb_filter,), name='{}_b'.format(self.name))
return (W, b)

def reinit_weights(self):
(W, b) = self.init_weights()
self.trainable_weights[0].set_value(W.get_value())
self.trainable_weights[1].set_value(b.get_value())

@property
def output_shape(self):
length = conv_output_length(self.input_shape[1],
Expand Down Expand Up @@ -227,6 +238,7 @@ def __init__(self, nb_filter, nb_row, nb_col,
border_mode='valid', subsample=(1, 1), dim_ordering='th',
W_regularizer=None, b_regularizer=None, activity_regularizer=None,
W_constraint=None, b_constraint=None, **kwargs):
self.resettable = True

if border_mode not in {'valid', 'same'}:
raise Exception('Invalid border mode for Convolution2D:', border_mode)
Expand Down Expand Up @@ -261,8 +273,9 @@ def build(self):
self.W_shape = (self.nb_row, self.nb_col, stack_size, self.nb_filter)
else:
raise Exception('Invalid dim_ordering: ' + self.dim_ordering)
self.W = self.init(self.W_shape, name='{}_W'.format(self.name))
self.b = K.zeros((self.nb_filter,), name='{}_b'.format(self.name))

(self.W, self.b) = self.init_weights()

self.trainable_weights = [self.W, self.b]
self.regularizers = []

Expand All @@ -282,6 +295,16 @@ def build(self):
self.set_weights(self.initial_weights)
del self.initial_weights

def init_weights(self):
W = self.init(self.W_shape, name='{}_W'.format(self.name))
b = K.zeros((self.nb_filter,), name='{}_b'.format(self.name))
return (W, b)

def reinit_weights(self):
(W, b) = self.init_weights()
self.trainable_weights[0].set_value(W.get_value())
self.trainable_weights[1].set_value(b.get_value())

@property
def output_shape(self):
input_shape = self.input_shape
Expand Down Expand Up @@ -403,6 +426,8 @@ def __init__(self, nb_filter, kernel_dim1, kernel_dim2, kernel_dim3,
border_mode='valid', subsample=(1, 1, 1), dim_ordering='th',
W_regularizer=None, b_regularizer=None, activity_regularizer=None,
W_constraint=None, b_constraint=None, **kwargs):
self.resettable = True

if K._BACKEND != 'theano':
raise Exception(self.__class__.__name__ +
' is currently only working with Theano backend.')
Expand Down Expand Up @@ -444,8 +469,8 @@ def build(self):
else:
raise Exception('Invalid dim_ordering: ' + self.dim_ordering)

self.W = self.init(self.W_shape, name='{}_W'.format(self.name))
self.b = K.zeros((self.nb_filter,), name='{}_b'.format(self.name))
(self.W, self.b) = self.init_weights()

self.trainable_weights = [self.W, self.b]
self.regularizers = []

Expand All @@ -465,6 +490,16 @@ def build(self):
self.set_weights(self.initial_weights)
del self.initial_weights

def init_weights(self):
W = self.init(self.W_shape, name='{}_W'.format(self.name))
b = K.zeros((self.nb_filter,), name='{}_b'.format(self.name))
return (W, b)

def reinit_weights(self):
(W, b) = self.init_weights()
self.trainable_weights[0].set_value(W.get_value())
self.trainable_weights[1].set_value(b.get_value())

@property
def output_shape(self):
input_shape = self.input_shape
Expand Down Expand Up @@ -560,7 +595,7 @@ def _pooling_function(self, back_end, inputs, pool_size, strides,

def get_output(self, train=False):
X = self.get_input(train)
X = K.expand_dims(X, -1) # add dummy last dimension
X = K.expand_dims(X, -1) # add dummy last dimension
X = K.permute_dimensions(X, (0, 2, 1, 3))
output = self._pooling_function(inputs=X, pool_size=self.pool_size,
strides=self.st,
Expand Down
104 changes: 78 additions & 26 deletions keras/layers/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,8 @@ class Dense(Layer):
def __init__(self, output_dim, init='glorot_uniform', activation='linear', weights=None,
W_regularizer=None, b_regularizer=None, activity_regularizer=None,
W_constraint=None, b_constraint=None, input_dim=None, **kwargs):
self.resettable = True

self.init = initializations.get(init)
self.activation = activations.get(activation)
self.output_dim = output_dim
Expand All @@ -1009,12 +1011,9 @@ def __init__(self, output_dim, init='glorot_uniform', activation='linear', weigh
super(Dense, self).__init__(**kwargs)

def build(self):
input_dim = self.input_shape[1]
self.input_dim = self.input_shape[1]

self.W = self.init((input_dim, self.output_dim),
name='{}_W'.format(self.name))
self.b = K.zeros((self.output_dim,),
name='{}_b'.format(self.name))
(self.W, self.b) = self.init_weights()

self.trainable_weights = [self.W, self.b]

Expand All @@ -1035,6 +1034,20 @@ def build(self):
self.set_weights(self.initial_weights)
del self.initial_weights

def init_weights(self):
W = self.init((self.input_dim, self.output_dim),
name='{}_W'.format(self.name))
b = K.zeros((self.output_dim,),
name='{}_b'.format(self.name))

return (W, b)

def reinit_weights(self):
new_weights = self.init_weights()

self.trainable_weights[0].set_value(new_weights[0].get_value())
self.trainable_weights[1].set_value(new_weights[1].get_value())

@property
def output_shape(self):
return (self.input_shape[0], self.output_dim)
Expand Down Expand Up @@ -1105,6 +1118,8 @@ def __init__(self, output_dim,
W_regularizer=None, b_regularizer=None, activity_regularizer=None,
W_constraint=None, b_constraint=None,
input_dim=None, input_length=None, **kwargs):
self.resettable = True

self.output_dim = output_dim
self.init = initializations.get(init)
self.activation = activations.get(activation)
Expand All @@ -1126,14 +1141,12 @@ def __init__(self, output_dim,
super(TimeDistributedDense, self).__init__(**kwargs)

def build(self):
input_dim = self.input_shape[2]
self.input_dim = self.input_shape[2]

self.W = self.init((input_dim, self.output_dim),
name='{}_W'.format(self.name))
self.b = K.zeros((self.output_dim,),
name='{}_b'.format(self.name))
(self.W, self.b) = self.init_weights()

self.trainable_weights = [self.W, self.b]

self.regularizers = []

if self.W_regularizer:
Expand All @@ -1152,6 +1165,20 @@ def build(self):
self.set_weights(self.initial_weights)
del self.initial_weights

def init_weights(self):
W = self.init((self.input_dim, self.output_dim),
name='{}_W'.format(self.name))
b = K.zeros((self.output_dim,),
name='{}_b'.format(self.name))

return (W, b)

def reinit_weights(self):
new_weights = self.init_weights()

self.trainable_weights[0].set_value(new_weights[0].get_value())
self.trainable_weights[1].set_value(new_weights[1].get_value())

@property
def output_shape(self):
input_shape = self.input_shape
Expand Down Expand Up @@ -1414,6 +1441,8 @@ def __init__(self, output_dim, nb_feature=4,
init='glorot_uniform', weights=None,
W_regularizer=None, b_regularizer=None, activity_regularizer=None,
W_constraint=None, b_constraint=None, input_dim=None, **kwargs):
self.resettable = True

self.output_dim = output_dim
self.nb_feature = nb_feature
self.init = initializations.get(init)
Expand All @@ -1433,14 +1462,11 @@ def __init__(self, output_dim, nb_feature=4,
super(MaxoutDense, self).__init__(**kwargs)

def build(self):
input_dim = self.input_shape[1]

self.W = self.init((self.nb_feature, input_dim, self.output_dim),
name='{}_W'.format(self.name))
self.b = K.zeros((self.nb_feature, self.output_dim),
name='{}_b'.format(self.name))
self.input_dim = self.input_shape[1]
(self.W, self.b) = self.init_weights()

self.trainable_weights = [self.W, self.b]

self.regularizers = []

if self.W_regularizer:
Expand All @@ -1459,6 +1485,19 @@ def build(self):
self.set_weights(self.initial_weights)
del self.initial_weights

def init_weights(self):
W = self.init((self.nb_feature, self.input_dim, self.output_dim),
name='{}_W'.format(self.name))
b = K.zeros((self.nb_feature, self.output_dim),
name='{}_b'.format(self.name))
return (W, b)

def reinit_weights(self):
new_weights = self.init_weights()

self.trainable_weights[0].set_value(new_weights[0].get_value())
self.trainable_weights[1].set_value(new_weights[1].get_value())

@property
def output_shape(self):
return (self.input_shape[0], self.output_dim)
Expand Down Expand Up @@ -1989,6 +2028,8 @@ def __init__(self, init='glorot_uniform', transform_bias=-2,
activation='linear', weights=None,
W_regularizer=None, b_regularizer=None, activity_regularizer=None,
W_constraint=None, b_constraint=None, input_dim=None, **kwargs):
self.resettable = True

self.init = initializations.get(init)
self.transform_bias = transform_bias
self.activation = activations.get(activation)
Expand All @@ -2009,17 +2050,9 @@ def __init__(self, init='glorot_uniform', transform_bias=-2,
super(Highway, self).__init__(**kwargs)

def build(self):
input_dim = self.input_shape[1]
self.input_dim = self.input_shape[1]

self.W = self.init((input_dim, input_dim),
name='{}_W'.format(self.name))
self.W_carry = self.init((input_dim, input_dim),
name='{}_W_carry'.format(self.name))

self.b = K.zeros((input_dim,), name='{}_b'.format(self.name))
# initialize with a vector of values `transform_bias`
self.b_carry = K.variable(np.ones((input_dim,)) * self.transform_bias,
name='{}_b_carry'.format(self.name))
(self.W, self.b, self.W_carry, self.b_carry) = self.init_weights()

self.trainable_weights = [self.W, self.b, self.W_carry, self.b_carry]

Expand All @@ -2040,6 +2073,25 @@ def build(self):
self.set_weights(self.initial_weights)
del self.initial_weights

def init_weights(self):
W = self.init((self.input_dim, self.input_dim),
name='{}_W'.format(self.name))
W_carry = self.init((self.input_dim, self.input_dim),
name='{}_W_carry'.format(self.name))

b = K.zeros((self.input_dim,), name='{}_b'.format(self.name))
# initialize with a vector of values `transform_bias`
b_carry = K.variable(np.ones((self.input_dim,)) * self.transform_bias,
name='{}_b_carry'.format(self.name))

return (W, b, W_carry, b_carry)

def reinit_weights(self):
new_weights = self.init_weights()

for i in range(len(self.trainable_weights)):
self.trainable_weights[i].set_value(new_weights[i].get_value())

@property
def output_shape(self):
return (self.input_shape[0], self.input_shape[1])
Expand Down
13 changes: 11 additions & 2 deletions keras/layers/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def __init__(self, input_dim, output_dim,
W_constraint=None,
mask_zero=False,
weights=None, dropout=0., **kwargs):
self.resettable = True

self.input_dim = input_dim
self.output_dim = output_dim
self.init = initializations.get(init)
Expand All @@ -73,8 +75,7 @@ def __init__(self, input_dim, output_dim,
def build(self):
self.input = K.placeholder(shape=(self.input_shape[0], self.input_length),
dtype='int32')
self.W = self.init((self.input_dim, self.output_dim),
name='{}_W'.format(self.name))
self.W = self.init_weights()
self.trainable_weights = [self.W]
self.regularizers = []
if self.W_regularizer:
Expand All @@ -88,6 +89,14 @@ def build(self):
if self.initial_weights is not None:
self.set_weights(self.initial_weights)

def init_weights(self):
return self.init((self.input_dim, self.output_dim),
name='{}_W'.format(self.name))

def reinit_weights(self):
W = self.init_weights()
self.trainable_weights[0].set_value(W.get_value())

def get_output_mask(self, train=None):
X = self.get_input(train)
if not self.mask_zero:
Expand Down
Loading