padding = int((kSize - 1)/2)
时间: 2023-05-14 09:04:31 浏览: 74
这是一个计算卷积核大小的公式,其中 kSize 是卷积核的大小,padding 是填充的大小。这个公式可以用来计算在进行卷积操作时需要进行的填充大小。具体的计算方法是将卷积核大小减去 1,然后除以 2,再向下取整即可得到填充大小。
相关问题
File "/root/Desktop/EAST-master/multigpu_train.py", line 180, in <module> tf.app.run() File "/root/miniconda3/envs/txy2/lib/python3.7/site-packages/tensorflow_core/python/platform/app.py", line 40, in run _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef) File "/root/miniconda3/envs/txy2/lib/python3.7/site-packages/absl/app.py", line 312, in run _run_main(main, args) File "/root/miniconda3/envs/txy2/lib/python3.7/site-packages/absl/app.py", line 258, in _run_main sys.exit(main(argv)) File "/root/Desktop/EAST-master/multigpu_train.py", line 110, in main total_loss, model_loss = tower_loss(iis, isms, igms, itms, reuse_variables) File "/root/Desktop/EAST-master/multigpu_train.py", line 30, in tower_loss f_score, f_geometry = model.model(images, is_training=True) File "/root/Desktop/EAST-master/model.py", line 77, in model spp_output = spp_layer(f[0]) File "/root/Desktop/EAST-master/model.py", line 44, in spp_layer strides=[1, strides[0], strides[1], 1], padding='VALID') File "/root/miniconda3/envs/txy2/lib/python3.7/site-packages/tensorflow_core/python/ops/nn_ops.py", line 3815, in max_pool name=name) File "/root/miniconda3/envs/txy2/lib/python3.7/site-packages/tensorflow_core/python/ops/gen_nn_ops.py", line 5662, in max_pool ksize = [_execute.make_int(_i, "ksize") for _i in ksize] File "/root/miniconda3/envs/txy2/lib/python3.7/site-packages/tensorflow_core/python/ops/gen_nn_ops.py", line 5662, in <listcomp> ksize = [_execute.make_int(_i, "ksize") for _i in ksize] File "/root/miniconda3/envs/txy2/lib/python3.7/site-packages/tensorflow_core/python/eager/execute.py", line 169, in make_int (arg_name, repr(v))) TypeError: Expected int for argument 'ksize' not <tf.Tensor 'model_0/feature_fusion/SpatialPyramidPooling/strided_slice_2:0' shape=() dtype=int32>. Process finished with exit code 1
这个错误是由于传递给函数的参数 ksize 需要是整数类型,但是你传递了一个 Tensor 对象。你需要确保将 Tensor 转换为整数类型后再传递给函数。你可以使用 TensorFlow 的 `tf.cast()` 函数将 Tensor 转换为整数类型,例如:`tf.cast(ksize_tensor, tf.int32)`。你需要找到代码中使用了 `tf.nn.max_pool()` 函数的部分,并检查是否在调用该函数时传递了一个 Tensor 类型的 ksize 参数,如果是,则需要将其转换为整数类型。例如,你可以将以下代码:
```
pool = tf.nn.max_pool(input, ksize=ksize_tensor, strides=[1, strides[0], strides[1], 1], padding='VALID')
```
修改为:
```
ksize = tf.cast(ksize_tensor, tf.int32)
pool = tf.nn.max_pool(input, ksize=[1, ksize, ksize, 1], strides=[1, strides[0], strides[1], 1], padding='VALID')
```
这应该可以解决你的问题。
cnn-crf命名实体识别代码
以下是一个使用CNN-CRF模型进行命名实体识别的代码示例:
```python
import tensorflow as tf
import numpy as np
class CNN_CRF(object):
def __init__(self, sequence_length, num_classes, vocab_size, embedding_size, filter_sizes, num_filters, hidden_dim, l2_reg_lambda=0.0):
self.input_x = tf.placeholder(tf.int32, [None, sequence_length], name="input_x")
self.input_y = tf.placeholder(tf.int32, [None, sequence_length], name="input_y")
self.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob")
self.sequence_length = sequence_length
l2_loss = tf.constant(0.0)
with tf.name_scope("embedding"):
self.W = tf.Variable(tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0), name="W")
self.embedded_chars = tf.nn.embedding_lookup(self.W, self.input_x)
self.embedded_chars_expanded = tf.expand_dims(self.embedded_chars, -1)
pooled_outputs = []
for i, filter_size in enumerate(filter_sizes):
with tf.name_scope("conv-maxpool-%s" % filter_size):
filter_shape = [filter_size, embedding_size, 1, num_filters]
W = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1), name="W")
b = tf.Variable(tf.constant(0.1, shape=[num_filters]), name="b")
conv = tf.nn.conv2d(
self.embedded_chars_expanded,
W,
strides=[1, 1, 1, 1],
padding="VALID",
name="conv")
h = tf.nn.relu(tf.nn.bias_add(conv, b), name="relu")
pooled = tf.nn.max_pool(
h,
ksize=[1, sequence_length - filter_size + 1, 1, 1],
strides=[1, 1, 1, 1],
padding='VALID',
name="pool")
pooled_outputs.append(pooled)
num_filters_total = num_filters * len(filter_sizes)
h_pool = tf.concat(pooled_outputs, 3)
self.h_pool_flat = tf.reshape(h_pool, [-1, num_filters_total])
with tf.name_scope("dropout"):
self.h_drop = tf.nn.dropout(self.h_pool_flat, self.dropout_keep_prob)
with tf.name_scope("output"):
W = tf.get_variable(
"W",
shape=[num_filters_total, hidden_dim],
initializer=tf.contrib.layers.xavier_initializer())
b = tf.Variable(tf.constant(0.1, shape=[hidden_dim]), name="b")
self.hidden_output = tf.nn.xw_plus_b(self.h_drop, W, b, name="hidden_output")
self.hidden_output_expanded = tf.expand_dims(self.hidden_output, -1)
with tf.name_scope("crf"):
self.transition_params = tf.Variable(tf.random_uniform([num_classes, num_classes], -0.1, 0.1), name="transition_params")
self.log_likelihood, self.transition_params = tf.contrib.crf.crf_log_likelihood(
self.hidden_output,
self.input_y,
self.sequence_length,
self.transition_params)
self.loss = tf.reduce_mean(-self.log_likelihood)
with tf.name_scope("accuracy"):
self.prediction = tf.argmax(self.hidden_output, 1, name="prediction")
correct_predictions = tf.equal(self.prediction, tf.argmax(self.input_y, 1))
self.accuracy = tf.reduce_mean(tf.cast(correct_predictions, "float"), name="accuracy")
self.params = [param for param in tf.trainable_variables() if 'embedding' not in param.name]
l2_reg_lambda = tf.constant(l2_reg_lambda)
l2_loss += tf.nn.l2_loss(W)
l2_loss += tf.nn.l2_loss(b)
self.loss += l2_reg_lambda * l2_loss
```
这里使用的是TensorFlow框架,模型结构包括了卷积神经网络和条件随机场。其中,卷积神经网络用于提取输入句子中的特征,而条件随机场则用于对提取到的特征进行标注,得到最终的命名实体识别结果。
阅读全文