old_contents: #!/usr/bin/env python
  
  import climate
  import matplotlib.pyplot as plt
  import numpy as np
  import numpy.random as rng
  import theanets
  
  climate.enable_default_logging()
  
  S = np.linspace(0, 4 * np.pi, 256)
  
  def wave(i=0):
      return (0.4 * np.sin(S) + 0.3 * np.sin(i * S / 2))[:, None, None]
  
  def waves(n=64):
      return np.concatenate([wave(rng.randint(15, 30)) for _ in range(n)], axis=1).astype('f')
  
  # set up a network and train it using some sinusoidal data.
  
  e = theanets.Experiment(theanets.recurrent.Regressor, layers=(2, 10, 1))
  
  def sum_waves():
      x = waves()
      y = waves()
      return [np.concatenate([x, y], axis=2), x + y]
  
  e.train(sum_waves, batch_size=64, train_batches=16)
  
  # use the network to predict a novel output.
  
  batch = np.zeros((len(S), e.args.batch_size, 2)).astype('f')
  batch[:, :1, 0:1] = wave(13).astype('f')
  batch[:, :1, 1:2] = wave(14).astype('f')
  
  predict = e.network.predict(batch)[:, 0, :]
  
  # plot the input, output, and error of the network.
  
  def plot(x, label):
      ax.plot(x.flatten(), '.-', label=label)
  
  ax = plt.subplot(111)
  ax.xaxis.tick_bottom()
  ax.yaxis.tick_left()
  for loc, spine in ax.spines.items():
      if loc in 'left bottom':
          spine.set_position(('outward', 6))
      elif loc in 'right top':
          spine.set_color('none')
  
  plot(batch[:, 0, 0] + batch[:, 0, 1], 'Target')
  plot(predict, 'Prediction')
  
  ax.set_xlim(0, len(S))
  ax.set_xlabel('Time')
  ax.set_ylabel('Amplitude')
  
  plt.legend()
  plt.show()
  
new_contents: #!/usr/bin/env python
  
  import climate
  import logging
  import matplotlib.pyplot as plt
  import numpy as np
  import numpy.random as rng
  import theanets
  
  climate.enable_default_logging()
  
  BATCH_SIZE = 32
  T = np.linspace(0, 1, 100)
  
  def pair():
      c = lambda a, b: np.concatenate([a, b], axis=2).astype('f')
      f = np.exp(rng.uniform(0, 1, size=BATCH_SIZE))
      t = np.outer(T, np.ones(BATCH_SIZE))[..., None]
      s = np.outer(np.ones_like(T), f)[..., None]
      z = 2 * np.pi * s * t
      return c(t, s), c(np.sin(z), np.cos(z))
  
  src, tgt = pair()
  logging.info('data batches: %s -> %s', src.shape, tgt.shape)
  
  e = theanets.Experiment(
      theanets.recurrent.Regressor, layers=(2, 10, 2), batch_size=BATCH_SIZE)
  e.train(pair)
  prd = e.network.predict(src)
  
  for i in range(3):
      ax = plt.subplot(3, 1, i + 1)
      ax.plot(tgt[:, i, 0].flatten(), '--', color='#1f77b4', label='Target Sine')
      ax.plot(tgt[:, i, 1].flatten(), '--', color='#2ca02c', label='Target Cosine')
      ax.plot(prd[:, i, 0].flatten(), '-', color='#1f77b4', lw=2, label='Predicted Sine')
      ax.plot(prd[:, i, 1].flatten(), '-', color='#2ca02c', lw=2, label='Predicted Cosine')
      ax.xaxis.tick_bottom()
      ax.yaxis.tick_left()
      ax.spines['top'].set_color('none')
      ax.spines['right'].set_color('none')
      ax.set_ylabel('Amplitude')
      if i == 0:
          plt.legend()
      if i == 2:
          ax.set_xlabel('Time')
      else:
          ax.set_xticks([])
  plt.show()
  
subject: Change recurrent-sinusoid to produce sine values from frequency input.