gpt4 book ai didi

python - 格式化大字符串时出错 - 如何检测错误?

转载 作者:太空宇宙 更新时间:2023-11-04 03:46:26 25 4
gpt4 key购买 nike

我是 Python 的新手,目前我正在处理我正在使用的库所需的大字符串格式。

问题的发生是因为我不明白在大字符串格式中错误到底发生在哪里。更准确地说,我得到了一个形式的错误

Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2731, in run_code
exec code_obj in self.user_global_ns, self.user_ns
File "<ipython-input-3-f6a3bb7fe2f9>", line 13, in <module>
trainCV = trainCV % (train_params)
ValueError: unsupported format character ',' (0x2c) at index 2726

有没有办法精确检测出错误发生的那一行?

我的完整代码如下所示:

trainCV = open('Conv_Constructor.yaml','r').read()
train_params = {'batch_size': 100,
'output_channels_h2': 64,
'conv_kernel_size': 8,
'pool_size': 2,
'stride_size': 1,
'output_channels_h3': 64,
'num_classes': 6,
'valid_stop': 4200,
'test_start': 4200,
'test_stop': 4400,
'max_epochs': 5}

trainCV = trainCV % (train_params)
print trainCV

我尝试格式化为字符串的 Conv_Constructor.yaml 文件如下

#  ---------- INPUTS ---------
#
# batch_size
# output_channels_h2
# conv_kernel_size
# pool_size
# stride_size
# output_channels_h3
# num_classes
# valid_stop
# test_start
# test_stop
# max_epochs

##################################################################

!obj:pylearn2.train.Train {
dataset: !obj:pylearn2.official_train_data.load_data {
start: 0,
stop: 4000
# one_hot: 1,
},

model: !obj:pylearn2.models.mlp.MLP {
batch_size: %(batch_size)i,
input_space: !obj:pylearn2.space.Conv2DSpace {
shape: [32, 32],
num_channels: 1,
axes = ('b',0,1,'c')
},

layers: [ !obj:pylearn2.models.mlp.ConvRectifiedLinear {
layer_name: 'h2',
output_channels: %(output_channels_h2)i,
#params : !pkl: 'dae_layer_1_weights.plk',
irange: .05,

kernel_shape: [%(conv_kernel_size)i, %(conv_kernel_size)i],

pool_shape: [%(pool_size)i, %(pool_size)i],

pool_stride: [%(stride_size)i, %(stride_size)i],

max_kernel_norm: 1.9365
}, !obj:pylearn2.models.mlp.ConvRectifiedLinear {
layer_name: 'h3',
output_channels: %(output_channels_h3)i,
#params : !pkl: 'dae_layer_1_weights.plk',
irange: .05,
kernel_shape: %(conv_kernel_size)i, %(conv_kernel_size)i],

pool_shape:[%(pool_size)i, %(pool_size)i],

pool_stride: [%(stride_size)i, %(stride_size)i],
max_kernel_norm: 1.9365


}, !obj:pylearn2.models.mlp.Softmax {
max_col_norm: 1.9365,
layer_name: 'y',
n_classes: %(num_classes)i,
istdev: .05
}
],
},

algorithm: !obj:pylearn2.training_algorithms.sgd.SGD {
batch_size: %(batch_size)i,
learning_rate: .01,
init_momentum: .5,
monitoring_dataset:
{
'valid' : !obj:pylearn2.official_train_data.load_data {
start: 4000,
stop: %(valid_stop)i
#one_hot: 1,
},
'test' : !obj:pylearn2.official_train_data.load_data {
start: %(test_start),
stop: %(test_stop)
#one_hot: 1,
}
},

cost: !obj:pylearn2.costs.cost.SumOfCosts { costs: [
!obj:pylearn2.costs.cost.MethodCost {
method: 'cost_from_X'
}, !obj:pylearn2.costs.mlp.WeightDecay {
coeffs: [ .00005, .00005, .00005 ]
}
]
},
termination_criterion: !obj:pylearn2.termination_criteria.And {
criteria: [
!obj:pylearn2.termination_criteria.MonitorBased {
channel_name: "valid_y_misclass",
prop_decrease: 0.50,
N: 50
},
!obj:pylearn2.termination_criteria.EpochCounter {
max_epochs: %(max_epochs)i
},
]
},
},

extensions:
[ !obj:pylearn2.train_extensions.best_params.MonitorBasedSaveBest {
channel_name: 'valid_y_misclass',
save_path: "%(save_path)s/convolutional_network_best.pkl"
}, !obj:pylearn2.training_algorithms.sgd.MomentumAdjustor {
start: 1,
saturate: 10,
final_momentum: .99
}
]
}

最佳答案

通过单独处理每一行而不是整个字符串,您可以更轻松地定位错误。替换

trainCV = trainCV % (train_params)

trainCV = trainCV.split('\n')
t1=[]
try:
for i, t in enumerate(trainCV):
t1.append(t % (train_params))
except :
print 'Error in line {}:'.format(i)
print t[i]
raise

你会得到如下输出:

78
start: %(test_start),

意思是你的字符串格式不太好(在这种情况下,我认为括号后面有 i)。以这种方式调试你的大字符串,你应该有一个工作代码。

完成后,您可以通过加入列表来打印它:

print '\n'.join(t1)

关于python - 格式化大字符串时出错 - 如何检测错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23628181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com