infer的时候 报格式错误
Created by: JessieMeng
I0919 10:07:53.975118 19210 Util.cpp:166] commandline: --use_gpu=False
haha
I0919 10:08:41.787912 19210 GradientMachine.cpp:85] Initing parameters..
I0919 10:08:46.817989 19210 GradientMachine.cpp:92] Init parameters done.
Traceback (most recent call last):
File "testtestauc.py", line 151, in <module>
main()
File "testtestauc.py", line 142, in main
input=batch)
File "/home/disk3/mengziyang/paddle/python27/lib/python2.7/site-packages/paddle/v2/inference.py", line 133, in infer
return inferer.infer(field=field, input=input, feeding=feeding)
File "/home/disk3/mengziyang/paddle/python27/lib/python2.7/site-packages/paddle/v2/inference.py", line 74, in infer
for result in self.iter_infer_field(field=field, **kwargs):
File "/home/disk3/mengziyang/paddle/python27/lib/python2.7/site-packages/paddle/v2/inference.py", line 61, in iter_infer_field
for result in self.iter_infer(**kwargs):
File "/home/disk3/mengziyang/paddle/python27/lib/python2.7/site-packages/paddle/v2/inference.py", line 54, in iter_infer
yield self.__gradient_machine__.forwardTest(feeder(data_batch))
File "/home/disk3/mengziyang/paddle/python27/lib/python2.7/site-packages/py_paddle/dataprovider_converter.py", line 278, in __call__
return self.convert(dat, argument)
File "/home/disk3/mengziyang/paddle/python27/lib/python2.7/site-packages/paddle/v2/data_feeder.py", line 134, in convert
return DataProviderConverter.convert(self, reorder_data(dat), argument)
File "/home/disk3/mengziyang/paddle/python27/lib/python2.7/site-packages/py_paddle/dataprovider_converter.py", line 263, in convert
scanner.pre_scan(each_step)
File "/home/disk3/mengziyang/paddle/python27/lib/python2.7/site-packages/py_paddle/dataprovider_converter.py", line 220, in pre_scan
for each in dat:
TypeError: 'int' object is not iterable
报的错误 代码如下:
import paddle.v2 as paddle
import cPickle
import copy
from movielens import *
import time
import numpy as np
import random
fout = open("all.test.result","w")
def AUC(records):
records = sorted(records, key = lambda x: x[1], reverse = True)
auc = 0.0
fp1, tp1, fp2, tp2 = 0.0, 0.0, 0.0, 0.0
for record in records:
fp2 += (1-record[0]) #noclick
tp2 += record[0] #click
auc += (fp2 - fp1) * (tp2 + tp1)
fp1, tp1 = fp2, tp2
return auc / (2.0 * tp2 * fp2) if tp2 * fp2 > 0.0 else 0.5
def get_mov_combined_features():
#movie_title_dict = get_movie_title_dict()
uid = paddle.layer.data(
name='user_id',
type=paddle.data_type.integer_value(
user_size() + 1))
usr_emb = paddle.layer.embedding(input=uid, size=32)
mov_id = paddle.layer.data(
name='movie_id',
type=paddle.data_type.integer_value(
max_movie_id()+ 1))
mov_emb = paddle.layer.embedding(input=mov_id, size=32)
#mov_fc = paddle.layer.fc(input=mov_emb, size=32)
mov_category = paddle.layer.data(
name='category',
type=paddle.data_type.integer_value(3))
mov_cate_emb = paddle.layer.embedding(input=mov_category,size=32)
#mov_categories_hidden = paddle.layer.fc(input=mov_cate_emb, size=32)
mov_title_id = paddle.layer.data(
name='movie_title',
type=paddle.data_type.integer_value_sequence(id_size()+1))
mov_title_emb = paddle.layer.embedding(input=mov_title_id, size=32)
#mov_title_conv = paddle.networks.sequence_conv_pool(
# input=mov_title_emb, hidden_size=32, context_len=3)
mov_seq_pool = paddle.layer.pooling(input=mov_title_emb,pooling_type=paddle.pooling.Sum())
mov_pic_attent_id = paddle.layer.data(
name='movie_pic_attent',
type=paddle.data_type.integer_value_sequence(73621))
mov_pic_attent_emb = paddle.layer.embedding(input=mov_pic_attent_id, size=32)
mov_pic_attent_pool = paddle.layer.pooling(input=mov_pic_attent_emb,pooling_type=paddle.pooling.Sum())
mov_artical_attent_id = paddle.layer.data(
name='movie_artical_attent',
type=paddle.data_type.integer_value_sequence(186671))
mov_artical_attent_emb = paddle.layer.embedding(input=mov_artical_attent_id, size=32)
mov_artical_attent_pool = paddle.layer.pooling(input=mov_artical_attent_emb,pooling_type=paddle.pooling.Sum())
user_word_feature_id = paddle.layer.data(
name='user_word_feature',
type=paddle.data_type.integer_value_sequence(186671))
user_word_feature_emb = paddle.layer.embedding(input=user_word_feature_id, size=32)
user_word_feature_pool = paddle.layer.pooling(input=user_word_feature_emb,pooling_type=paddle.pooling.Sum())
mov_combined_features = paddle.layer.fc(
input=[usr_emb,user_word_feature_pool,mov_emb,mov_cate_emb,mov_seq_pool,mov_pic_attent_pool,mov_artical_attent_pool],
size=200,
act=paddle.activation.Relu())
return mov_combined_features
#return [mov_emb,mov_cate_emb,mov_title_emb]
def main():
paddle.init(use_gpu=False)
#usr_combined_features = get_usr_combined_features()
mov_combined_features = get_mov_combined_features()
layer1 = paddle.layer.fc(input=mov_combined_features,size=200, act=paddle.activation.Relu())
#layer2 = paddle.layer.fc(input=layer1,size=200, act=paddle.activation.Relu())
#layer3 = paddle.layer.fc(input=layer2,size=200, act=paddle.activation.Relu())
#layer4 = paddle.layer.fc(input=layer3,size=200, act=paddle.activation.Relu())
#layer5 = paddle.layer.fc(input=layer4,size=200, act=paddle.activation.Relu())
#layer6 = paddle.layer.fc(input=layer5,size=200, act=paddle.activation.Relu())
#layer7 = paddle.layer.fc(input=layer6,size=200, act=paddle.activation.Relu())
output = paddle.layer.fc(input=layer1,size=1,act=paddle.activation.Sigmoid())
#output = paddle.layer.fc(input=[usr_combined_features,mov_combined_features],size=1,act=paddle.activation.Sigmoid())
#label_data = paddle.layer.data(name="score", type=paddle.data_type.integer_value(2))
label_data = paddle.layer.data(name="score", type=paddle.data_type.dense_vector(1))
print "haha"
#cost = paddle.layer.cross_entropy_cost(
# input=output,
# label=label_data)
cost = paddle.layer.multi_binary_label_cross_entropy_cost(
input=output,
label=label_data)
#eval = paddle.evaluator.auc(
# input=output,
# label=label_data)
parameters = paddle.parameters.create(cost)
trainer = paddle.trainer.SGD(
cost=cost,
# extra_layers=eval,
parameters=parameters,
update_equation=paddle.optimizer.Adam(learning_rate=1e-4))
feeding = {
'user_id': 0,
'user_word_feature':1,
'movie_id': 2,
'category': 3,
'movie_title': 4,
'movie_pic_attent': 5,
'movie_artical_attent':6,
'score': 7
}
infer_reader=paddle.batch(
paddle.reader.buffered(
test(), size=8192),
batch_size=25600)
pass_num=0
while(pass_num<2):
batch_num=0
while(batch_num<7600):
with open('params_pass_%d_%d.tar' % (pass_num,batch_num), 'r') as f:
parameters2 = paddle.parameters.Parameters.from_tar(f)
records=[]
for id,batch in enumerate(infer_reader()):
if id > 10 :
break
labels=[]
probs = paddle.infer(output_layer=output,
parameters=parameters2,
input=batch)
for index,record in enumerate(batch):
labels.append(record[-1])
for index in xrange(len(probs)):
records.append([labels[index][0],probs[index][0]])
print "params_pass_%d_%d,Test auc:%s" % (pass_num,batch_num,str(AUC(records)))
batch_num+=500
pass_num+=1
if __name__ == '__main__':
main()