theano.config.floatX报float64错误

本文介绍了一种常见的Keras与Theano环境配置问题,即由于默认浮点类型不匹配导致的TypeError。通过调整Theano的配置参数floatX为float32,可以有效解决此问题,使代码在不同环境下顺利运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我实际用的环境是Keras,查错误时查到是Theano的配置问题,所以在标题里就写成Theano的问题了,

  是这样的,从Github上下载的别人的代码,准备复现别人的实验,结果在机器上部署好环境之后跑代码时报错为:

  TypeError: ('An update must have the same type as the original shared variable (shared_var=weight_1, 
shared_var.type=TensorType(float32, matrix), update_val=Elemwise{sub,no_inplace}.0, 
update_val.type=TensorType(float64, matrix)).', 'If the difference is related to the broadcast pattern, you 
can call the tensor.unbroadcast(var, axis_to_unbroadcast[, ...]) function to remove broadcastable 
dimensions.')

  意思就是代码里float的位数不对,以前的Keras版本都是float32的,现在的新版Keras默认是float64的,具体两者有什么区别还不太清楚,但是表现在执行的时候就是报上面的错,之后程序停止,

  解决方案:在代码中,import语句的下面添加下面一行代码,

   theano.config.floatX= 'float32'

  之后运行就行了,此时就是显式设置相关编译信息,

 

  (初来乍到,有错误之处还望指正,其实个人刚开始使用Keras和Theano,有很多也不太了解,)

“class ResConv1DLayer(object): def __init__(self, rng, input, n_in=0, n_out=0, halfWinSize=0, dilation=1, activation=T.nnet.relu, mask=None): self.input = input self.n_in = n_in self.n_out = n_out self.halfWinSize = halfWinSize windowSize = 2*halfWinSize + 1 self.filter_size = windowSize self.dilation = (1, dilation) in4conv2D = input.dimshuffle(0, 1, 'x', 2) w_shp = (n_out, n_in, 1, windowSize) if activation == T.nnet.relu: W_values = np.asarray(rng.normal(scale=np.sqrt(2. / (n_in*windowSize + n_out)), size = w_shp), dtype=theano.config.floatX ) else: W_values = np.asarray( rng.uniform(low = - np.sqrt(6. / (n_in*windowSize + n_out)), high = np.sqrt(6. / (n_in*windowSize + n_out)), size = w_shp), dtype=theano.config.floatX ) if activation == T.nnet.sigmoid: W_values *= 4 self.W = theano.shared(value=W_values, name='ResConv1d_W', borrow=True) b_shp = (n_out,) self.b = theano.shared(np.asarray( rng.uniform(low=-.0, high=.0, size=b_shp), dtype=input.dtype), name ='ResConv1d_b', borrow=True) if dilation > 1: conv_out = T.nnet.conv2d(in4conv2D, self.W, filter_shape=w_shp, border_mode='half', filter_dilation=(1, dilation) ) else: conv_out = T.nnet.conv2d(in4conv2D, self.W, filter_shape=w_shp, border_mode='half') if activation is not None: conv_out_bias = activation(conv_out + self.b.dimshuffle('x', 0, 'x', 'x')) else: conv_out_bias = (conv_out + self.b.dimshuffle('x', 0, 'x', 'x')) out2 = conv_out_bias.dimshuffle(0, 1, 3, 2)[:, :, :, 0] if mask is not None: out2_sub = out2[:, :, :mask.shape[1] ] mask_new = mask.dimshuffle(0, 'x', 1) self.output = T.set_subtensor(out2_sub, T.mul(out2_sub, mask_new) ) else: self.output = out2 self.params=[self.W, self.b] self.paramL1 = abs(self.W).sum() + abs(self.b).sum() self.paramL2 = (self.W**2).sum() + (self.b**2).sum()”将这段theano代码转成torch的
03-20
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值