我正在使用ImageDataGenerator. 我需要将每个灰度图像的内容复制到 3 个通道中。我尝试了以下代码,但似乎不起作用:
def grayscale_to_rgb(images, channel_axis=-1):
images= K.expand_dims(images, axis=channel_axis)
tiling = [1] * 4 # 4 dimensions: B, H, W, C
tiling[channel_axis] *= 3
images= K.tile(images, tiling)
return images
train_images_orign= grayscale_to_rgb(train_images_orign)
valid_images_orign= grayscale_to_rgb(valid_images_orign)
test_images_orign= grayscale_to_rgb(test_images_orign)
x_train, y_train = next(train_images_orign)
x_valid, y_valid = next(valid_images_orign)
x_test, y_test = next(test_images_orign)
Run Code Online (Sandbox Code Playgroud)
我应该朝哪个方向来实现这一目标?
我对更新 Keras 中现有的图层参数感兴趣(不是删除图层并插入一个新图层,而只是修改现有参数)。
我将举一个我正在编写的函数的例子:
def add_filters(self, model):
conv_indices = [i for i, layer in enumerate(model.layers) if 'convolution' in layer.get_config()['name']]
random_conv_index = random.randint(0, len(conv_indices)-1)
factor = 2
conv_layer = model.layers[random_conv_index]
conv_layer.filters = conv_layer.filters * factor
print('new conv layer filters after transform is:', conv_layer.filters)
print('just to make sure, its:', model.layers[random_conv_index].filters)
return model
Run Code Online (Sandbox Code Playgroud)
所以这里基本上发生的事情是我从我的网络中随机抽取一个卷积层(我所有的卷积层的名称中都有“卷积”)并尝试将过滤器加倍。据我所知,在任何情况下,这都不会导致输入/输出大小兼容性的任何“编译问题”。
问题是,我的模型根本没有改变。我最后添加的 2 个打印输出打印了正确的数字(是之前过滤器数量的两倍)。但是当我编译模型并打印model.summary()时,我仍然看到了之前的过滤量。
顺便说一句,我并不局限于 Keras。如果有人知道如何使用 PyTorch 实现这一点,我也会购买它:D
python neural-network deep-learning conv-neural-network keras
我从这里得到以下例子。
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
'data/train',
target_size=(150, 150),
batch_size=32,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
'data/validation',
target_size=(150, 150),
batch_size=32,
class_mode='binary')
Run Code Online (Sandbox Code Playgroud)
有两个单独的目录用于训练和验证。只是好奇我是否可以从同一目录而不是两个单独的目录中分离出训练和验证数据?有什么例子吗?
我正在用Keras进行二进制分类
loss='binary_crossentropy',optimizer=tf.keras.optimizers.Adam最后一层是keras.layers.Dense(1, activation=tf.nn.sigmoid)。
据我所知,loss价值是在训练阶段用来评估模型的。但是,当我Keras对测试数据集使用模型评估时(例如m_recall.evaluate(testData,testLabel),也有一些loss值,并附带accuracy以下输出所示的值)
test size: (1889, 18525)
1889/1889 [==============================] - 1s 345us/step
m_acc: [0.5690245978371045, 0.9523557437797776]
1889/1889 [==============================] - 1s 352us/step
m_recall: [0.24519687695911097, 0.9359449444150344]
1889/1889 [==============================] - 1s 350us/step
m_f1: [0.502442331737344, 0.9216516675489677]
1889/1889 [==============================] - 1s 360us/step
metric name: ['loss', 'acc']
Run Code Online (Sandbox Code Playgroud)
loss测试期间的意义/用途是什么?为什么它是如此之高(如0.5690在m_acc)?准确度评估对我来说似乎不错(例如,0.9523在中m_acc),但我也很担心loss,这是否会使我的模型表现不佳?
PS
m_acc,m_recall等等都只是我的名字我的模型的方法(它们是由不同的指标受训GridSearchCV)
更新:
我只是意识到loss …
我正在创建自定义的激活功能,尤其是RBF激活功能:
from keras import backend as K
from keras.layers import Lambda
l2_norm = lambda a,b: K.sqrt(K.sum(K.pow((a-b),2), axis=0, keepdims=True))
def rbf2(x):
X = #here i need inputs that I receive from previous layer
Y = # here I need weights that I should apply for this layer
l2 = l2_norm(X,Y)
res = K.exp(-1 * gamma * K.pow(l2,2))
return res
Run Code Online (Sandbox Code Playgroud)
该函数rbf2接收上一层作为输入:
#some keras layers
model.add(Dense(84, activation='tanh')) #layer1
model.add(Dense(10, activation = rbf2)) #layer2
Run Code Online (Sandbox Code Playgroud)
我应该怎么做才能获得输入layer1和权重layer2以创建定制的激活功能?
我实际上想做的是为LeNet5神经网络实现输出层。LeNet-5的输出层有点特殊,而不是计算输入和权重向量的点积,每个神经元输出其输入向量与其权重向量之间的欧几里得距离的平方。
例如,layer1 …
python machine-learning conv-neural-network keras activation-function
我正在 Keras 中实现这里描述的 LSTM 架构。我想我真的很接近,尽管我仍然对共享层和特定于语言的层的组合有疑问。这是公式(大约):y = g * y^s + (1 - g) * y^u
这是我试过的代码:
### Linear Layers ###
univ_linear = Dense(50, activation=None, name='univ_linear')
univ_linear_en = univ_linear(en_encoded)
univ_linear_es = univ_linear(es_encoded)
print(univ_linear_en)
# Gate >> g
gate_en = Dense(50, activation='sigmoid', name='gate_en')(en_encoded)
gate_es = Dense(50, activation='sigmoid', name='gate_es')(es_encoded)
print(gate_en)
print(gate_es)
# EN >> y^s
spec_linear_en = Dense(50, activation=None, name='spec_linear_en') (en_encoded)
print(spec_linear_en)
# g * y^s
gated_spec_linear_en = Multiply()([gate_en, spec_linear_en])
print(gated_spec_linear_en)
# ES >> y^s
spec_linear_es = Dense(50, activation=None, name='spec_linear_es')(es_encoded) …Run Code Online (Sandbox Code Playgroud) 我有一个由一些密集层组成的 Keras Sequential 模型。我将整个模型的可训练属性设置为 False。但是我看到各个层的可训练属性仍然设置为 True。我是否需要单独将图层的可训练属性也设置为 False?那么在整个模型上将trainable property设置为False是什么意思呢?
我正在训练我的 Keras 模型,以使用提供的数据参数预测它是否会出手,并且它将以 0 表示否,1 表示是的方式表示。但是,当我尝试预测它时,我得到的值是浮动的。
我尝试使用与火车数据完全相同的数据来获得 1,但它不起作用。
我使用下面的数据尝试了one-hot编码。
https://github.com/eijaz1/Deep-Learning-in-Keras-Tutorial/blob/master/keras_tutorial.ipynb
import pandas as pd
from keras.utils import to_categorical
from keras.models import load_model
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import EarlyStopping
#read in training data
train_df_2 = pd.read_csv('diabetes_data.csv')
#view data structure
train_df_2.head()
#create a dataframe with all training data except the target column
train_X_2 = train_df_2.drop(columns=['diabetes'])
#check that the target variable has been removed
train_X_2.head()
#one-hot encode target column
train_y_2 = to_categorical(train_df_2.diabetes)
#vcheck that target column has …Run Code Online (Sandbox Code Playgroud) 我一直在尝试使用Keras中的神经网络实现一个简单的线性回归模型,希望了解我们如何在Keras库中工作.不幸的是,我最终得到了一个非常糟糕的模型.这是实施:
from pylab import *
from keras.models import Sequential
from keras.layers import Dense
#Generate dummy data
data = data = linspace(1,2,100).reshape(-1,1)
y = data*5
#Define the model
def baseline_model():
model = Sequential()
model.add(Dense(1, activation = 'linear', input_dim = 1))
model.compile(optimizer = 'rmsprop', loss = 'mean_squared_error', metrics = ['accuracy'])
return model
#Use the model
regr = baseline_model()
regr.fit(data,y,epochs =200,batch_size = 32)
plot(data, regr.predict(data), 'b', data,y, 'k.')
Run Code Online (Sandbox Code Playgroud)
生成的图如下:
有人可以指出上述模型定义中的缺陷(可以确保更好的拟合)吗?
python machine-learning linear-regression neural-network keras