小编ZFT*_*rbo的帖子

如何告诉Keras根据损失值停止训练?

目前我使用以下代码:

callbacks = [
    EarlyStopping(monitor='val_loss', patience=2, verbose=0),
    ModelCheckpoint(kfold_weights_path, monitor='val_loss', save_best_only=True, verbose=0),
]
model.fit(X_train.astype('float32'), Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
      shuffle=True, verbose=1, validation_data=(X_valid, Y_valid),
      callbacks=callbacks)
Run Code Online (Sandbox Code Playgroud)

它告诉Keras在2个时期的损失没有改善时停止训练.但是我希望在损失变得小于某些常数"THR"之后停止训练:

if val_loss < THR:
    break
Run Code Online (Sandbox Code Playgroud)

我在文档中看到有可能进行自己的回调:http: //keras.io/callbacks/ 但是没有找到如何停止训练过程.我需要一个建议.

python machine-learning neural-network conv-neural-network keras

70
推荐指数
4
解决办法
4万
查看次数

如何在Keras模型中替换(或插入)中间层?

我有一个训练有素的Keras模型,我想要:

1)用相同但无偏差的Con2D层替换。

2)在首次激活之前添加BatchNormalization层

我怎样才能做到这一点?

def keras_simple_model():
    from keras.models import Model
    from keras.layers import Input, Dense,  GlobalAveragePooling2D
    from keras.layers import Conv2D, MaxPooling2D, Activation

    inputs1 = Input((28, 28, 1))
    x = Conv2D(4, (3, 3), activation=None, padding='same', name='conv1')(inputs1)
    x = Activation('relu')(x)
    x = Conv2D(4, (3, 3), activation=None, padding='same', name='conv2')(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool1')(x)

    x = Conv2D(8, (3, 3), activation=None, padding='same', name='conv3')(x)
    x = Activation('relu')(x)
    x = Conv2D(8, (3, 3), activation=None, padding='same', name='conv4')(x)
    x = Activation('relu')(x)
    x = …
Run Code Online (Sandbox Code Playgroud)

keras

16
推荐指数
2
解决办法
6739
查看次数

如何找到与其他 2 个字符串相似的字符串(就 Levenshtein 距离而言)?

假设我有 2 个非常相似的字符串。我想找到在 Levenshtein 距离方面接近 s1 和 s2 的其他字符串。

import Levenshtein
s1 = 'aaabbbccc'
s2 = 'abacbbbccde'
dist = Levenshtein.distance(s1, s2)
print(dist)
mid_str = get_avg_string(s1, s2)
Run Code Online (Sandbox Code Playgroud)

什么可以有效实现功能:

def get_avg_string(s1, s2):
    return ''
Run Code Online (Sandbox Code Playgroud)

我需要这个变量:

sum_lev = Levenshtein.distance(s1, mid_str) + Levenshtein.distance(s2, mid_str)
diff_lev = abs(Levenshtein.distance(s1, mid_str) - Levenshtein.distance(s2, mid_str)
Run Code Online (Sandbox Code Playgroud)

最小(我认为sum_lev将等于distdiff_lev <= 1)。

python string similarity levenshtein-distance

7
推荐指数
2
解决办法
291
查看次数

具有换档操作的二进制掩模,无循环

我们有一些大的二进制数N(大意味着数百万的数字).我们还有二进制掩码M,其中1表示我们必须删除数字N中此位置的数字,并将所有较高位移到一个位置右侧.

例:

N = 100011101110
M = 000010001000
Res   1000110110
Run Code Online (Sandbox Code Playgroud)

是否有可能在没有一些逻辑或算术运算的循环的情况下解决这个问题?我们可以假设我们可以在Python中访问bignum算法.

感觉它应该是这样的:Res = N - (N xor M)但它不起作用

UPD:我目前的循环解决方案如下:

def prepare_reduced_arrays(dict_of_N, mask):
    '''
    mask: string '0000011000'
    each element of dict_of_N - big python integer
    '''

    capacity = len(mask)
    answer = dict()
    for el in dict_of_N:
        answer[el] = 0

    new_capacity = 0
    for i in range(capacity - 1, -1, -1):
        if mask[i] == '1':
            continue
        cap2 = (1 << new_capacity)
        pos = (capacity - i - 1)
        for el in …
Run Code Online (Sandbox Code Playgroud)

python math binary logical-operators

5
推荐指数
1
解决办法
645
查看次数

如果可以激活多个输出,softmax层的替代品是什么?

例如,我有CNN试图从MNIST数据集中预测数字(使用Keras编写的代码).它有10个输出,形成softmax层.只有一个输出可以为真(每个数字从0到9独立):

Real: [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
Predicted: [0.02, 0.9, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01]
Run Code Online (Sandbox Code Playgroud)

由于softmax的定义,预测总和等于1.0.

假设我有一个任务,我需要对一些可能属于以下类别的对象进行分类:

Real: [0, 1, 0, 1, 0, 1, 0, 0, 0, 1]
Run Code Online (Sandbox Code Playgroud)

所以我需要以其他方式规范化.我需要在范围[0,1]上给出值的函数,其总和可以大于1.

我需要这样的东西:

Predicted: [0.1, 0.9, 0.05, 0.9, 0.01, 0.8, 0.1, 0.01, 0.2, 0.9]
Run Code Online (Sandbox Code Playgroud)

每个数字是对象属于给定类别的概率.之后,我可以使用像0.5这样的阈值来区分给定对象所属的类别.

出现以下问题:

  1. 那么哪个激活功能可用于此?
  2. 可能这个功能已经存在于Keras?
  3. 在这种情况下,您可以提出一些其他预测方法吗?

mnist conv-neural-network keras softmax

4
推荐指数
1
解决办法
2904
查看次数

什么是在Windows中正确替换posix_memalign?

我目前正在尝试在Windows中构建word2vec.但posix_memalign()功能有问题 .每个人都建议使用_aligned_malloc(),但参数的数量是不同的.那么什么是posix_memalign()Windows中最好的等价物?

c c++ memory windows posix

3
推荐指数
2
解决办法
5624
查看次数