Go语言报错cannot define new methods on non-local type

今天在写一段Golang代码时报错:

package main
import(
  "fmt"
)
func Add(a ,b int){         //函数
  fmt.Println(a+b)
}
func (a int) Add (b int){   //方法
  fmt.Println(a+b)
}

报错信息如下:

cannot define new methods on non-local type int

上网搜了下原来原因是 go语言不允许为简单的内置类型添加方法

这里需要用type来临时定义一个和int具有同样功能的类型。不过需要强调的是,这个类型不能看成是int类型的别名,它们属于不同的类型,不能直接相互赋值。

修改后的代码如下:

package main
import(
  "fmt"
)
type myInt int
func Add(a ,b int){             //函数
  fmt.Println(a+b)
}
func (a myInt) Add (b myInt){   //方法
  fmt.Println(a+b)
}
func main() {
        a, b := 3,4
        var aa,bb myInt = 3,4
        Add(a,b)
        aa.Add(bb)
  }
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Non-local Neural Networks (NLNet) 是一个用于图像和视频处理的深度学习模型,它在处理长距离的空间关系时表现出色。下面是一个使用 TensorFlow 2 实现的 NLNet 模型的示例代码: ```python import tensorflow as tf from tensorflow.keras.layers import Conv2D, BatchNormalization, Activation, Add, Input, Lambda, Concatenate from tensorflow.keras.models import Model from tensorflow.keras.regularizers import l2 def non_local_block(x, compression=2, mode='embedded_gaussian', reg=l2(0.)): ''' Non-local block with optional compression and different modes: - 'dot_product': dot product similarity (original paper) - 'embedded_gaussian': embedded Gaussian similarity (default) - 'concatenation': concatenation-based similarity (not recommended) ''' # Get input shape and channels input_shape = tf.shape(x) channels = input_shape[-1] # Define theta, phi, and g theta = Conv2D(channels // compression, 1, use_bias=False, kernel_regularizer=reg)(x) phi = Conv2D(channels // compression, 1, use_bias=False, kernel_regularizer=reg)(x) g = Conv2D(channels // compression, 1, use_bias=False, kernel_regularizer=reg)(x) # Reshape theta and phi to (N, H*W, C') theta = Lambda(lambda x: tf.reshape(x, (input_shape[0], -1, channels // compression)))(theta) phi = Lambda(lambda x: tf.reshape(x, (input_shape[0], -1, channels // compression)))(phi) # Compute similarity between theta and phi if mode == 'dot_product': similarity = Lambda(lambda x: tf.matmul(x[0], x[1], transpose_b=True))([theta, phi]) elif mode == 'embedded_gaussian': theta = Lambda(lambda x: tf.expand_dims(x, 2))(theta) phi = Lambda(lambda x: tf.expand_dims(x, 1))(phi) theta_phi = Add()([theta, phi]) f = Conv2D(1, 1, use_bias=False, kernel_regularizer=reg)(theta_phi) f = Activation('softmax')(f) similarity = Lambda(lambda x: tf.matmul(x[0], x[1]))([f, g]) elif mode == 'concatenation': theta_phi = Concatenate(axis=-1)([theta, phi]) h = Conv2D(channels // compression, 1, use_bias=False, kernel_regularizer=reg)(theta_phi) similarity = Lambda(lambda x: tf.matmul(x[0], x[1], transpose_b=True))([h, g]) # Reshape similarity to (N, H, W, C') similarity = Lambda(lambda x: tf.reshape(x, (input_shape[0], input_shape[1], input_shape[2], channels // compression)))(similarity) # Compute output y = Conv2D(channels, 1, use_bias=False, kernel_regularizer=reg)(similarity) y = Add()([y, x]) y = BatchNormalization()(y) y = Activation('relu')(y) return y def build_nlnet(input_shape, num_classes, num_blocks=5, compression=2, mode='embedded_gaussian', reg=l2(0.)): ''' Build NLNet model with optional number of blocks, compression, and mode. ''' # Define input inputs = Input(shape=input_shape) # Initial convolution x = Conv2D(64, 3, padding='same', use_bias=False, kernel_regularizer=reg)(inputs) x = BatchNormalization()(x) x = Activation('relu')(x) # Non-local blocks for i in range(num_blocks): x = non_local_block(x, compression=compression, mode=mode, reg=reg) # Final convolution and pooling x = Conv2D(128, 1, use_bias=False, kernel_regularizer=reg)(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2D(num_classes, 1, use_bias=False, kernel_regularizer=reg)(x) x = BatchNormalization()(x) x = Activation('softmax')(x) x = Lambda(lambda x: tf.reduce_mean(x, axis=(1, 2)))(x) # Define model model = Model(inputs=inputs, outputs=x) return model ``` 此代码实现了一个用于图像分类的 NLNet 模型,其中包含多个非局部块。该模型使用可配置的压缩因子和模式,并支持 L2 正则化。要使用此代码,请调用 `build_nlnet()` 函数并传递输入形状、类别数以及其他可选参数。例如: ```python input_shape = (224, 224, 3) num_classes = 1000 model = build_nlnet(input_shape, num_classes, num_blocks=5, compression=2, mode='embedded_gaussian', reg=l2(0.0001)) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值