本文整理汇总了Python中keras.backend._BACKEND属性的典型用法代码示例。如果您正苦于以下问题:Python backend._BACKEND属性的具体用法?Python backend._BACKEND怎么用?Python backend._BACKEND使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类keras.backend
的用法示例。
在下文中一共展示了backend._BACKEND属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import _BACKEND [as 别名]
def __init__(self, layer, input_shape=None, input_dim=None, input_length=None, weights=None, **kwargs):
if K._BACKEND == 'tensorflow':
import warnings
warnings.warn('TimeDistributed() wrapper untested with tensorflow! Use at your own risk.')
self.layer = layer
self.initial_weights = weights
self.input_dim = input_dim
self.input_length = input_length
if hasattr(self.layer, 'input_ndim'):
self.input_ndim = self.layer.input_ndim + 1
if input_shape:
self.set_input_shape((None, ) + input_shape)
if self.input_dim:
kwargs['input_shape'] = (self.input_length, self.input_dim)
super(TimeDistributed, self).__init__(**kwargs)
示例2: get_output
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import _BACKEND [as 别名]
def get_output(self, train=False):
def format_shape(shape):
if K._BACKEND == 'tensorflow':
def trf(x):
try:
return int(x)
except TypeError:
return x
return map(trf, shape)
return shape
X = self.get_input(train)
in_shape = format_shape(K.shape(X))
batch_flatten_len = K.prod(in_shape[:2])
cast_in_shape = (batch_flatten_len, ) + tuple(in_shape[i] for i in range(2, K.ndim(X)))
pre_outs = self.layer(K.reshape(X, cast_in_shape))
out_shape = format_shape(K.shape(pre_outs))
cast_out_shape = (in_shape[0], in_shape[1]) + tuple(out_shape[i] for i in range(1, K.ndim(pre_outs)))
outputs = K.reshape(pre_outs, cast_out_shape)
return outputs
示例3: add_vgg_to_layer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import _BACKEND [as 别名]
def add_vgg_to_layer(input_layer, trainable=False, pool_mode='max',
weights_path='vgg16_weights.h5', last_layer=None):
layers = get_layers(pool_mode=pool_mode, last_layer=last_layer,
trainable=trainable, weights_path=weights_path)
# load the weights of the VGG16 networks
assert os.path.exists(weights_path), 'Model weights not found (see "--vgg-weights" parameter).'
f = h5py.File(weights_path)
last_layer = input_layer
for k, layer in zip(range(f.attrs['nb_layers']), layers):
g = f['layer_{}'.format(k)]
weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])]
if isinstance(layer, Convolution2D) and K._BACKEND == 'theano':
weights[0] = np.array(weights[0])[:, :, ::-1, ::-1]
last_layer = layer(last_layer)
layer.trainable = trainable
layer.set_weights(weights)
f.close()
return last_layer
示例4: get_callbacks
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import _BACKEND [as 别名]
def get_callbacks(config_data, appendix=''):
ret_callbacks = []
model_stored = False
callbacks = config_data['callbacks']
if K._BACKEND == 'tensorflow':
tensor_board = TensorBoard(log_dir=os.path.join('logging', config_data['tb_log_dir']), histogram_freq=10)
ret_callbacks.append(tensor_board)
for callback in callbacks:
if callback['name'] == 'early_stopping':
ret_callbacks.append(EarlyStopping(monitor=callback['monitor'], patience=callback['patience'], verbose=callback['verbose'], mode=callback['mode']))
elif callback['name'] == 'model_checkpoit':
model_stored = True
path = config_data['output_path']
basename = config_data['output_basename']
base_path = os.path.join(path, basename)
opath = os.path.join(base_path, 'best_model{}.h5'.format(appendix))
save_best = bool(callback['save_best_only'])
ret_callbacks.append(ModelCheckpoint(filepath=opath, verbose=callback['verbose'], save_best_only=save_best, monitor=callback['monitor'], mode=callback['mode']))
return ret_callbacks, model_stored
示例5: build
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import _BACKEND [as 别名]
def build(self, input_shape):
self.input_spec = [InputSpec(ndim=3)]
if K._BACKEND == 'tensorflow':
if not input_shape[1]:
raise Exception('When using TensorFlow, you should define '
'explicitly the number of timesteps of '
'your sequences.\n'
'If your first layer is an Embedding, '
'make sure to pass it an "input_length" '
'argument. Otherwise, make sure '
'the first layer has '
'an "input_shape" or "batch_input_shape" '
'argument, including the time axis.')
if not self.layer.built:
self.layer.build(input_shape)
self.layer.built = True
super(ProbabilityTensor, self).build()
示例6: build_network
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import _BACKEND [as 别名]
def build_network(deepest=False):
dropout = [0., 0.1, 0.2, 0.3, 0.4]
conv = [(64, 3, 3), (128, 3, 3), (256, 3, 3), (512, 3, 3), (512, 2, 2)]
input= Input(shape=(3, 32, 32) if K._BACKEND == 'theano' else (32, 32,3))
output = fractal_net(
c=3, b=5, conv=conv,
drop_path=0.15, dropout=dropout,
deepest=deepest)(input)
output = Flatten()(output)
output = Dense(NB_CLASSES, init='he_normal')(output)
output = Activation('softmax')(output)
model = Model(input=input, output=output)
#optimizer = SGD(lr=LEARN_START, momentum=MOMENTUM)
#optimizer = SGD(lr=LEARN_START, momentum=MOMENTUM, nesterov=True)
optimizer = Adam()
#optimizer = Nadam()
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
plot(model, to_file='model.png', show_shapes=True)
return model
示例7: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import _BACKEND [as 别名]
def call(self, x, mask=None):
# input shape: (nb_samples, time (padded with zeros), input_dim)
# note that the .build() method of subclasses MUST define
# self.input_spec with a complete input shape.
input_shape = self.input_spec[0].shape
if K._BACKEND == 'tensorflow':
if not input_shape[1]:
raise Exception('When using TensorFlow, you should define '
'explicitly the number of timesteps of '
'your sequences.\n'
'If your first layer is an Embedding, '
'make sure to pass it an "input_length" '
'argument. Otherwise, make sure '
'the first layer has '
'an "input_shape" or "batch_input_shape" '
'argument, including the time axis. '
'Found input shape at layer ' + self.name +
': ' + str(input_shape))
if self.layer.stateful:
initial_states = self.layer.states
else:
initial_states = self.layer.get_initial_states(x)
constants = self.get_constants(x)
preprocessed_input = self.layer.preprocess_input(x)
last_output, outputs, states = K.rnn(self.step, preprocessed_input,
initial_states,
go_backwards=self.layer.go_backwards,
mask=mask,
constants=constants,
unroll=self.layer.unroll,
input_length=input_shape[1])
if self.layer.stateful:
self.updates = []
for i in range(len(states)):
self.updates.append((self.layer.states[i], states[i]))
if self.layer.return_sequences:
return outputs
else:
return last_output
示例8: set_img_format
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import _BACKEND [as 别名]
def set_img_format():
try:
if K.backend() == 'theano':
K.set_image_data_format('channels_first')
else:
K.set_image_data_format('channels_last')
except AttributeError:
if K._BACKEND == 'theano':
K.set_image_dim_ordering('th')
else:
K.set_image_dim_ordering('tf')
示例9: check_dtype
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import _BACKEND [as 别名]
def check_dtype(var, dtype):
if K._BACKEND == 'theano':
assert var.dtype == dtype
else:
assert var.dtype.name == '%s_ref' % dtype
示例10: main
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import _BACKEND [as 别名]
def main(net):
''' *WARNIING*
This model use Batch Normalization, so the prediction
is affected by batch. Use multiple, different data
samples together (at least 4) for reliable prediction.'''
print('Running main() with network: %s and backend: %s' % (net, K._BACKEND))
# setting
audio_paths = ['data/bensound-cute.mp3',
'data/bensound-actionable.mp3',
'data/bensound-dubstep.mp3',
'data/bensound-thejazzpiano.mp3']
melgram_paths = ['data/bensound-cute.npy',
'data/bensound-actionable.npy',
'data/bensound-dubstep.npy',
'data/bensound-thejazzpiano.npy']
# prepare data like this
melgrams = np.zeros((0, 1, 96, 1366))
if librosa_exists:
for audio_path in audio_paths:
melgram = ap.compute_melgram(audio_path)
melgrams = np.concatenate((melgrams, melgram), axis=0)
else:
for melgram_path in melgram_paths:
melgram = np.load(melgram_path)
melgrams = np.concatenate((melgrams, melgram), axis=0)
# load model like this
if net == 'cnn':
model = MusicTaggerCNN(weights='msd', include_top=False)
elif net == 'crnn':
model = MusicTaggerCRNN(weights='msd', include_top=False)
# predict the tags like this
print('Predicting features...')
start = time.time()
features = model.predict(melgrams)
print features[:, :10]
return
示例11: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import _BACKEND [as 别名]
def call(self, x, mask=None):
# input shape: (nb_samples, time (padded with zeros), input_dim)
# note that the .build() method of subclasses MUST define
# self.input_spec with a complete input shape.
input_shape = self.input_spec[0].shape
if K._BACKEND == 'tensorflow':
if not input_shape[1]:
raise Exception('When using TensorFlow, you should define '
'explicitly the number of timesteps of '
'your sequences.\n'
'If your first layer is an Embedding, '
'make sure to pass it an "input_length" '
'argument. Otherwise, make sure '
'the first layer has '
'an "input_shape" or "batch_input_shape" '
'argument, including the time axis. '
'Found input shape at layer ' + self.name +
': ' + str(input_shape))
if self.layer.stateful:
initial_states = self.layer.states
else:
initial_states = self.layer.get_initial_states(x)
constants = self.get_constants(x)
preprocessed_input = self.layer.preprocess_input(x)
last_output, outputs, states = K.rnn(self.step, preprocessed_input,
initial_states,
go_backwards=self.layer.go_backwards,
mask=mask,
constants=constants,
unroll=self.layer.unroll,
input_length=input_shape[1])
if self.layer.stateful:
self.updates = []
for i in range(len(states)):
self.updates.append((self.layer.states[i], states[i]))
if self.layer.return_sequences:
return outputs
else:
示例12: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import _BACKEND [as 别名]
def call(self, x, mask=None):
# input shape: (nb_samples, time (padded with zeros), input_dim)
# note that the .build() method of subclasses MUST define
# self.input_spec with a complete input shape.
input_shape = self.input_spec[0].shape
if K._BACKEND == 'tensorflow':
if not input_shape[1]:
raise Exception('When using TensorFlow, you should define '
'explicitly the number of timesteps of '
'your sequences.\n'
'If your first layer is an Embedding, '
'make sure to pass it an "input_length" '
'argument. Otherwise, make sure '
'the first layer has '
'an "input_shape" or "batch_input_shape" '
'argument, including the time axis. '
'Found input shape at layer ' + self.name +
': ' + str(input_shape))
if self.layer.stateful:
initial_states = self.layer.states
else:
initial_states = self.layer.get_initial_states(x)
constants = self.get_constants(x)
preprocessed_input = self.layer.preprocess_input(x)
last_output, outputs, states = K.rnn(self.step, preprocessed_input,
initial_states,
go_backwards=self.layer.go_backwards,
mask=mask,
constants=constants,
unroll=self.layer.unroll,
input_length=input_shape[1])
if self.layer.stateful:
self.updates = []
for i in range(len(states)):
self.updates.append((self.layer.states[i], states[i]))
if self.layer.return_sequences:
return outputs
else:
return last_output
示例13: build
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import _BACKEND [as 别名]
def build(self, input_shape=None):
'''Assumes that self.layer is already set.
Should be called at the end of .build() in the
children classes.
'''
ndim = len(input_shape)
assert ndim >= 3
self.input_spec = [InputSpec(ndim=str(ndim)+'+')]
#if input_shape is not None:
# self.last_two = input_shape[-2:]
self._input_shape = input_shape
#self.input_spec = [InputSpec(shape=input_shape)]
if K._BACKEND == 'tensorflow':
if not input_shape[1]:
raise Exception('When using TensorFlow, you should define '
'explicitly the number of timesteps of '
'your sequences.\n'
'If your first layer is an Embedding, '
'make sure to pass it an "input_length" '
'argument. Otherwise, make sure '
'the first layer has '
'an "input_shape" or "batch_input_shape" '
'argument, including the time axis.')
#child_input_shape = (np.prod(input_shape[:-2]),) + input_shape[-2:]
child_input_shape = (None,)+input_shape[-2:]
if not self.layer.built:
self.layer.build(child_input_shape)
self.layer.built = True
self.trainable_weights = getattr(self.layer, 'trainable_weights', [])
self.non_trainable_weights = getattr(self.layer, 'non_trainable_weights', [])
self.updates = getattr(self.layer, 'updates', [])
self.regularizers = getattr(self.layer, 'regularizers', [])
self.constraints = getattr(self.layer, 'constraints', {})
示例14: log_keras_version_info
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import _BACKEND [as 别名]
def log_keras_version_info():
import keras
logger.info("Keras version: " + keras.__version__)
from keras import backend as K
try:
backend = K.backend()
except AttributeError:
backend = K._BACKEND # pylint: disable=protected-access
if backend == 'theano':
import theano
logger.info("Theano version: " + theano.__version__)
logger.warning("Using Keras' theano backend is not supported! Expect to crash...")
elif backend == 'tensorflow':
import tensorflow
logger.info("Tensorflow version: " + tensorflow.__version__) # pylint: disable=no-member
示例15: __init__
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import _BACKEND [as 别名]
def __init__(self, log_dir='./logs', histogram_freq=0, write_graph=True):
super(TensorBoard, self).__init__()
if K._BACKEND != 'tensorflow':
raise Exception('TensorBoard callback only works '
'with the TensorFlow backend.')
self.log_dir = log_dir
self.histogram_freq = histogram_freq
self.merged = None
self.write_graph = write_graph