我可以手动将输入神经元插入Keras的隐藏层吗?

时间:2020-02-20 11:58:11

标签: python tensorflow keras conv-neural-network

我有一个CNN,我想潜入一些额外信息进入最后一层。

这是代码的简化版本。注意评论

def pw_gen(characters, length):

    """generate all characters combinations with selected length and export them to a text file"""

    # counting number of combinations according to a formula in documentation
    k = length
    n = len(characters) + k - 1
    comb_numb = math.factorial(n)/(math.factorial(n-length)*math.factorial(length))

    x = 0

    # first value
    percent = 5

    # step of percent done to display
    step = 5

    # 'step' % of combinations
    boundary_value = comb_numb/(100/step)

    try:
        # output text file
        with open("password_combinations.txt", "a+") as f:
            for p in itertools.product(characters, repeat=length):
                combination = ''.join(p)

                # write each combination and create a new line
                f.write(combination + '\n')
                x += 1

                if boundary_value <= x <= comb_numb:
                    print("{} % complete".format(percent))
                    percent += step
                    boundary_value += comb_numb/(100/step)

                elif x > comb_numb:
                    break

因此,我有一些有关输入图像的其他信息,这些信息可能会对网络有所帮助。可以将其视为可能不应该进行合理加权的线索。

该线索采用整数形式,从技术上讲,其形式为[0,inf),但实际上可能为[0,20]。

所以我的问题是

  1. 一般来讲,用NN体系结构表示提示的合适方法是什么。

  2. 我如何调整Keras模型以使其在实践中实现?

  3. 奖金:如果我愿意,是否可以防止后续的退出功能退出该新增功能?

1 个答案:

答案 0 :(得分:1)

这可以通过使用Keras的功能性API来实现:

def define_model():
    inputs = Input(input_shape=(...))
    hints = Input(input_shape=(...))

    x = Conv2D(32, (3,3))(inputs)
    x = Conv2D(32, (3,3))(x)
    x = MaxPooling2D((2,2))(x)
    x = Conv2D(64, (3,3))(x)
    x = Conv2D(64, (3,3))(x)
    x = MaxPooling2D((2,2))(x)
    x = Flatten()(x)

    x = Add()([x, hints])

    x = Dense(1024)(x)
    x = Dropout(rate=0.4)(x)
    outputs = Dense(168)(x)

    model = Model([inputs, hints], outputs)

    model.compile()
    return model

我不知道如何使用Keras保护它免于辍学。