Skip to content

Commit

Permalink
add Sonnet Linear Module
Browse files Browse the repository at this point in the history
  • Loading branch information
Asugawara committed Dec 4, 2020
1 parent 10a55ff commit 7908ccc
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions open_spiel/python/pytorch/dqn.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@

import collections
import random
import math
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from scipy.stats import truncnorm

from open_spiel.python import rl_agent

Expand Down Expand Up @@ -87,6 +89,38 @@ def __iter__(self):
return iter(self._data)


class SonnetLinear(nn.Module):
"""A Sonnet linear module.
Always includes biases and only supports ReLU activations.
"""
def __init__(self, in_size, out_size, activate_relu=True):
"""Creates a Sonnet linear layer.
Args:
in_size: (int) number of inputs
out_size: (int) number of outputs
activate_relu: (bool) whether to include a ReLU activation layer
"""

super(SonnetLinear, self).__init__()
self._activate_relu = activate_relu
stddev = 1.0 / math.sqrt(in_size)
mean = 0
lower = (-2 * stddev - mean) / stddev
upper = (2 * stddev - mean) / stddev
# Weight initialization inspired by Sonnet's Linear layer,
# which cites https://arxiv.org/abs/1502.03167v3
# pytorch default: initialized from uniform(-sqrt(1/in_features), sqrt(1/in_features))
self._weight = nn.Parameter(torch.Tensor(
truncnorm.rvs(lower, upper, loc=mean, scale=stddev, size=[out_size, in_size])))
self._bias = nn.Parameter(torch.zeros([out_size]))

def forward(self, tensor):
y = F.linear(tensor, self._weight, self._bias)
return F.relu(y) if self._activate_relu else y


class MLP(nn.Module):
"""A simple network built from nn.linear layers."""

Expand All @@ -108,14 +142,14 @@ def __init__(self,
self._layers = []
# Hidden layers
for size in hidden_sizes:
self._layers.append(nn.Linear(in_features=input_size, out_features=size))
self._layers.append(nn.ReLU())
self._layers.append(SonnetLinear(in_size=input_size, out_size=size))
input_size = size
# Output layer
self._layers.append(
nn.Linear(in_features=input_size, out_features=output_size))
if activate_final:
self._layers.append(nn.ReLU())
SonnetLinear(
in_size=input_size,
out_size=output_size,
activate_relu=activate_final))

self.model = nn.ModuleList(self._layers)

Expand Down

0 comments on commit 7908ccc

Please sign in to comment.