当前位置: 首页>>代码示例>>Python>>正文


Python OrderedDict.append方法代码示例

本文整理汇总了Python中theano.compat.python2x.OrderedDict.append方法的典型用法代码示例。如果您正苦于以下问题:Python OrderedDict.append方法的具体用法?Python OrderedDict.append怎么用?Python OrderedDict.append使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在theano.compat.python2x.OrderedDict的用法示例。


在下文中一共展示了OrderedDict.append方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _build_model

# 需要导入模块: from theano.compat.python2x import OrderedDict [as 别名]
# 或者: from theano.compat.python2x.OrderedDict import append [as 别名]

#.........这里部分代码省略.........
            updates = OrderedDict()
            zipped = zip(params, gradients, model_updates)
            for ndx, (param, gradient, accumulated_gradient) in enumerate(zipped):
                agrad = rho * accumulated_gradient + (1 - rho) * gradient ** 2
                effective_learning_rate = (learning_rate / (Tensor.sqrt(agrad) + eps))
                update, delta = Trainer._calc_update(effective_learning_rate, gradient, param, last_layer=last_layer)
                # delta = effective_learning_rate * gradient
                updates[param] = update
                updates[accumulated_gradient] = agrad

        elif strategy == 'RMSProp2':
            updates = OrderedDict()
            zipped = zip(params, gradients, model_updates)
            for ndx, (param, gradient, accumulated_gradient) in enumerate(zipped):
                agrad = rho * accumulated_gradient + gradient ** 2
                effective_learning_rate = (learning_rate / (Tensor.sqrt(agrad) + eps))
                update, delta = Trainer._calc_update(effective_learning_rate, gradient, param, last_layer=last_layer)
                # delta = effective_learning_rate * gradient
                updates[param] = update
                updates[accumulated_gradient] = agrad

        elif strategy == 'adaDelta':
            updates = OrderedDict()
            zipped = zip(params, gradients, model_updates, model_deltas)
            for ndx, (param, gradient, accumulated_gradient, accumulated_delta) in enumerate(zipped):
                agrad = rho * accumulated_gradient + (1 - rho) * gradient ** 2
                # delta = Tensor.sqrt((accumulated_delta + eps) / (agrad + eps)) * gradient
                step_size = Tensor.sqrt((accumulated_delta + eps) / (agrad + eps))
                update, delta = Trainer._calc_update(step_size, gradient, param, last_layer=last_layer)
                updates[param] = update
                updates[accumulated_gradient] = agrad
                updates[accumulated_delta] = rho * accumulated_delta + (1 - rho) * (delta ** 2)

        elif strategy == 'adam':
            updates = OrderedDict()
            zipped = zip(params, gradients, model_updates, model_deltas)
            for ndx, (param, gradient, accumulated_gradient, accumulated_delta) in enumerate(zipped):
                moment_1 = bias_1 * accumulated_gradient + (1 - bias_1) * gradient
                moment_2 = bias_2 * accumulated_delta + (1 - bias_2) * gradient ** 2
                corrected_moment_1 = moment_1 / Tensor.cast((1 - bias_1 ** t), theano.config.floatX)
                corrected_moment_2 = moment_2 / Tensor.cast((1 - bias_2 ** t), theano.config.floatX)
                g = corrected_moment_1 / (Tensor.sqrt(corrected_moment_2 + eps))
                update, delta = Trainer._calc_update(learning_rate, g, param, last_layer=last_layer)

                updates[param] = update
                updates[accumulated_gradient] = moment_1
                updates[accumulated_delta] = moment_2
        elif strategy == 'SGDCayley':
            updates = []
            for param, gradient in zip(params, gradients):

                if param.name == 'Wx_layer0' or param.name == 'Wy_layer0':
                    param_update = Trainer._calc_update(learning_rate, gradient, param, 'Cayley',
                                                        last_layer=last_layer)
                else:
                    param_update = Trainer._calc_update(learning_rate, gradient, param, 'Regular',
                                                        last_layer=last_layer)

                updates.append((param, param_update))

        else:
            msg = 'Unknown optimization strategy'
            OutputLog().write(msg)
            raise Exception(msg)

        print 'Building function'

        variance_hidden_x = Tensor.var(x_hidden, axis=0)
        variance_hidden_y = Tensor.var(y_hidden, axis=0)

        if moving_averages is not None:
            Trainer._add_moving_averages(moving_averages, updates, number_of_batches)

        update_mean = []
        update_var = []
        for param in params:
            if 'W' in param.name:
                OutputLog().write('Adding weight: {0}'.format(param.name))
                update_mean.append(Tensor.mean(abs(updates[param])))
                update_var.append(Tensor.var(abs(updates[param])))

        update_mean = Tensor.stacklists(update_mean)
        update_var = Tensor.stacklists(update_var)

        # Building the theano function
        # input : batch index
        # output : both losses
        # updates : gradient decent updates for all params
        # givens : replacing inputs for each iteration

        model = function(inputs=[t],
                         outputs=[Tensor.mean(loss_backward),
                                  Tensor.mean(loss_forward),
                                  Tensor.mean(variance_hidden_x),
                                  Tensor.mean(variance_hidden_y),
                                  x_hidden,
                                  y_hidden, update_mean, update_var] + regularizations + [t],
                         updates=updates)

        return model
开发者ID:aviveise,项目名称:double_encoder,代码行数:104,代码来源:trainer.py


注:本文中的theano.compat.python2x.OrderedDict.append方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。