本文整理汇总了Python中engine.Engine.forward方法的典型用法代码示例。如果您正苦于以下问题:Python Engine.forward方法的具体用法?Python Engine.forward怎么用?Python Engine.forward使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类engine.Engine
的用法示例。
在下文中一共展示了Engine.forward方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Car
# 需要导入模块: from engine import Engine [as 别名]
# 或者: from engine.Engine import forward [as 别名]
class Car(object):
# constructor
def __init__(self):
config = ConfigParser()
config.sections()
config.read('config.ini')
self.engineLeft = Engine(config.getint('EngineLeft', 'A'), config.getint('EngineLeft', 'B'), config.getint('EngineLeft', 'E'), 'EngineLeft')
self.engineRight = Engine(config.getint('EngineRight', 'A'), config.getint('EngineRight', 'B'), config.getint('EngineRight', 'E'), 'EngineRight')
# stop engine
def stop(self):
self.engineLeft.stop()
self.engineRight.stop()
# drive forward
def forward(self, speed):
self.engineLeft.forward(speed)
self.engineRight.forward(speed)
# drive backward
def backward(self, speed):
self.engineLeft.backward(speed)
self.engineRight.backward(speed)
# turn right
def turnRight(self, turn):
self.engineRight.setTurn(turn)
# turn left
def turnLeft(self, turn):
self.engineLeft.setTurn(turn)
示例2: main
# 需要导入模块: from engine import Engine [as 别名]
# 或者: from engine.Engine import forward [as 别名]
def main():
parser = argparse.ArgumentParser(description='testing script')
parser.add_argument('--data', type=str, default='data/negotiate',
help='location of the data corpus')
parser.add_argument('--unk_threshold', type=int, default=20,
help='minimum word frequency to be in dictionary')
parser.add_argument('--model_file', type=str,
help='pretrained model file')
parser.add_argument('--seed', type=int, default=1,
help='random seed')
parser.add_argument('--hierarchical', action='store_true', default=False,
help='use hierarchical model')
parser.add_argument('--bsz', type=int, default=16,
help='batch size')
parser.add_argument('--cuda', action='store_true', default=False,
help='use CUDA')
args = parser.parse_args()
device_id = utils.use_cuda(args.cuda)
utils.set_seed(args.seed)
corpus = data.WordCorpus(args.data, freq_cutoff=args.unk_threshold, verbose=True)
model = utils.load_model(args.model_file)
crit = Criterion(model.word_dict, device_id=device_id)
sel_crit = Criterion(model.item_dict, device_id=device_id,
bad_toks=['<disconnect>', '<disagree>'])
testset, testset_stats = corpus.test_dataset(args.bsz, device_id=device_id)
test_loss, test_select_loss = 0, 0
N = len(corpus.word_dict)
for batch in testset:
# run forward on the batch, produces output, hidden, target,
# selection output and selection target
out, hid, tgt, sel_out, sel_tgt = Engine.forward(model, batch, volatile=False)
# compute LM and selection losses
test_loss += tgt.size(0) * crit(out.view(-1, N), tgt).data[0]
test_select_loss += sel_crit(sel_out, sel_tgt).data[0]
test_loss /= testset_stats['nonpadn']
test_select_loss /= len(testset)
print('testloss %.3f | testppl %.3f' % (test_loss, np.exp(test_loss)))
print('testselectloss %.3f | testselectppl %.3f' % (test_select_loss, np.exp(test_select_loss)))