Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tf2使用textcnn网络问题 #9

Open
Smile-L-up opened this issue May 25, 2022 · 0 comments
Open

tf2使用textcnn网络问题 #9

Smile-L-up opened this issue May 25, 2022 · 0 comments

Comments

@Smile-L-up
Copy link

Smile-L-up commented May 25, 2022

tf1 版本转tf2问题,当不添加textcnn网络时,训练预测均没有问题。但是当加入textcnn时训练时loss与acc都不错,但是预测都是错误的。以下tf2实现的textcnn基本都是直接转的。此外我还尝试tf.keras.layers.Conv2D()以及conv1d实现。但是效果都不行,本来考虑是不是训练周期等参数问题,但是跟您的项目参数保持一致,训练出来的模型就是有问题(有进行dropout),所以想请教一下您。

def textcnn(x):
    pooled_outputs = []

    filter_sizes = [2, 3, 4, 5, 6, 7]
    inputs_expand = tf.expand_dims(x, -1)
    for filter_size in filter_sizes:
        filter_shape = [filter_size, 312, 1, 128]
        W = tf.Variable(tf.random.truncated_normal(filter_shape, stddev=0.1), dtype=tf.float32, name="W")
        b = tf.Variable(tf.constant(0.1, shape=[128]), dtype=tf.float32, name="b")
        conv = tf.nn.conv2d(
            inputs_expand,
            W,
            strides=[1, 1, 1, 1],
            padding="VALID",
            name="conv")
        # Apply nonlinearity
        h = tf.nn.relu(tf.nn.bias_add(conv, b), name="relu")
        # Maxpooling over the outputs
        pooled = tf.nn.max_pool(
            h,
            ksize=[1, 60 - filter_size + 1, 1, 1],
            strides=[1, 1, 1, 1],
            padding='VALID',
            name="pool")
        pooled_outputs.append(pooled)
    # Combine all the pooled features
    num_filters_total = 128 * len(filter_sizes)
    h_pool = tf.concat(pooled_outputs, 3)
    h_pool_flat = tf.reshape(h_pool, [-1, num_filters_total])

    return h_pool_flat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant