我正在使用 keras 构建一个用于图像分割的卷积神经网络,我想使用“反射填充”而不是“相同”填充,但我找不到在 keras 中做到这一点的方法。
inputs = Input((num_channels, img_rows, img_cols))
conv1=Conv2D(32,3,padding='same',kernel_initializer='he_uniform',data_format='channels_first')(inputs)
Run Code Online (Sandbox Code Playgroud)
有没有办法实现反射层并将其插入到 keras 模型中?
python padding keras zero-padding convolutional-neural-network
我正在使用 Keras(tensorflow 后端)并且想知道如何将多个嵌入层添加到 Keras Sequential 模型中。
更具体地说,我的数据集中有几列具有分类值,并且我已经考虑使用单热编码,但已确定分类项目的数量为数百,导致大量且过于稀疏的列集。在寻找解决方案时,我发现 Keras 的嵌入层似乎非常优雅地解决了这个问题。但是,大多数示例(和 Keras 文档)说明了一个非常简单的情况,其中包含一个 Embedding 层。
不幸的是,我不知道如何将多个 Embedding 层作为输入集成到单个模型中。
我的代码看起来像这样,但它不起作用,我猜测多个 Embedding 层按顺序操作(第一个 Embedding 层输入第二个,依此类推)而不是模型的多个输入源:
model = Sequential()
model.add(Embedding(500, 64, input_length=10)) # categorical col 1
model.add(Embedding(100, 64, input_length=10)) # categorical col 2
model.add(Embedding(500, 64, input_length=10)) # categorical col 3
model.add(Flatten...
model.add(Dense...
Run Code Online (Sandbox Code Playgroud)
我的问题是如何建立 Keras Sequential 模型,以便能够使用上面显示的三个嵌入层。第一层和最后一层之间的具体内容:
model = Sequential()
#
# What goes here?
#
model.add(Dense...
Run Code Online (Sandbox Code Playgroud)
我是在正确的轨道上,还是我的方法不正确,我需要以不同的方式建立模型?任何建议/示例表示赞赏!
我遇到过一种情况,其中平均值包含填充值。X
给定某种形状的张量(batch_size, ..., features)
,可能有零填充特征来获得相同的形状。
如何对X
(特征)的最终维度进行平均,但仅对非零条目进行平均?因此,我们将总和除以非零条目的数量。
输入示例:
x = [[[[1,2,3], [2,3,4], [0,0,0]],
[[1,2,3], [2,0,4], [3,4,5]],
[[1,2,3], [0,0,0], [0,0,0]],
[[1,2,3], [1,2,3], [0,0,0]]],
[[[1,2,3], [0,1,0], [0,0,0]],
[[1,2,3], [2,3,4], [0,0,0]],
[[1,2,3], [0,0,0], [0,0,0]],
[[1,2,3], [1,2,3], [1,2,3]]]]
# Desired output
y = [[[1.5 2.5 3.5]
[2. 2. 4. ]
[1. 2. 3. ]
[1. 2. 3. ]]
[[0.5 1.5 1.5]
[1.5 2.5 3.5]
[1. 2. 3. ]
[1. 2. 3. ]]]
Run Code Online (Sandbox Code Playgroud) 由于 RAM 内存的限制,我按照这些说明构建了一个生成器,该生成器绘制小批量并将它们传递到 Keras 的 fit_generator 中。但是即使我继承了序列,Keras 也无法使用多处理准备队列。
这是我的多处理生成器。
class My_Generator(Sequence):
def __init__(self, image_filenames, labels, batch_size):
self.image_filenames, self.labels = image_filenames, labels
self.batch_size = batch_size
def __len__(self):
return np.ceil(len(self.image_filenames) / float(self.batch_size))
def __getitem__(self, idx):
batch_x = self.image_filenames[idx * self.batch_size:(idx + 1) * self.batch_size]
batch_y = self.labels[idx * self.batch_size:(idx + 1) * self.batch_size]
return np.array([
resize(imread(file_name), (200, 200))
for file_name in batch_x]), np.array(batch_y)
Run Code Online (Sandbox Code Playgroud)
主要功能:
batch_size = 100
num_epochs = 10
train_fnames = []
mask_training = []
val_fnames = []
mask_validation …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用早期停止和模型检查点来保存最佳模型,同时训练深度卷积神经网络.但是,我收到以下错误:
callback.set_model(model)
AttributeError: 'list' object has no attribute 'set_model'
Run Code Online (Sandbox Code Playgroud)
到目前为止我的代码是:
model = Sequential()
###First block
model.add(Conv2D(100,kernel_size = (3,3),activation = 'relu',padding = 'same',input_shape=(12,11,1)))
model.add(Conv2D(100,kernel_size = (3,3),activation = 'relu',padding = 'same'))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Dropout(0.20))
###Second block
model.add(Conv2D(128,kernel_size = (3,3),activation = 'relu',padding = 'same'))
model.add(Conv2D(128,kernel_size = (3,3),activation = 'relu',padding = 'same'))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Dropout(0.10))
model.add(Flatten())
#model.add(Dense(100,activation = 'relu',kernel_regularizer=regularizers.l2(0.01)))
model.add(Dense(1000,activation = 'relu',kernel_regularizer=regularizers.l2(0.01)))
model.add(Dropout(0.30))
model.add(Dense(500,activation = 'relu',kernel_regularizer=regularizers.l2(0.01)))
model.add(Dropout(0.10))
#model.add(Dense(500,activation = 'relu',kernel_regularizer=regularizers.l2(0.01)))
#model.add(Dropout(0.15))
model.add(Dense(5,activation = 'softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
earlystop = [EarlyStopping(monitor='val_acc', min_delta=0.001, patience=5, …
Run Code Online (Sandbox Code Playgroud) keras ×5
python ×5
convolutional-neural-network ×1
keras-layer ×1
padding ×1
tensorflow ×1
zero-padding ×1