LSTM counting numbers I faced with an unexpected behavior of LSTM network. I reduced my troubled case to the simplest task, just count an occurrences of a certain number in random vector(it's only for a benchmark). Due to that fact that LSTM network should keep the global state of the process, I see no evidence why this task shouldn't been solved easily. Could you clarify, is there a problem in my code, or LSTM networks can't solve this kind of problems at all?
The code is below (Chainer framework is used):
import random

from chainer import Chain, Variable
import chainer.links as L
import chainer.functions as F
from chainer import optimizers
import numpy as np

def generate_random_vector():
    '''
    This function generates random vector of length 50 of [1,2,3] values with the number of
    3'th randomly selected from 5 to 45, The function returns this vector and the defined
    number of 3'th been divided by 1 (to scale it).

    Example:

    (array([[ 2.],
       [ 2.],
       [ 2.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 3.],
       [ 2.],
       [ 1.],
       [ 1.],
       [ 2.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 2.],
       [ 1.],
       [ 3.],
       [ 2.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 2.],
       [ 1.],
       [ 3.],
       [ 2.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 2.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 2.],
       [ 3.],
       [ 2.],
       [ 1.],
       [ 2.],
       [ 2.],
       [ 3.],
       [ 3.],
       [ 1.],
       [ 1.],
       [ 2.],
       [ 1.]], dtype=float32), array(0.1666666716337204, dtype=float32))

    It means that the vector above contains (1/0.16 = 6) of 3'th values
    '''
    len = 50
    y = random.randint(5,45)
    set_of_nums = random.sample([1,2]*10000,(len-y)) + [3]*y
    random.shuffle(set_of_nums)
    x = np.array(set_of_nums).reshape(-1,1).astype(np.float32)
    y = np.array(1./y).astype(np.float32)
    return x,y

class ToyLSTM(Chain):
    '''
        This toy LSTM network should count the number of 3'th in the vector and return this value
        at the end of the whole chain.

    '''
    def __init__(self,gpu=True):
        super(ToyLSTM, self).__init__(
            inp=L.LSTM(1, 5),  # the LSTM layer
            out=L.Linear(5, 1),  # the feed-forward output layer
        )
        self.optimizer = optimizers.SGD()
        self.optimizer.setup(self)

    def reset_state(self):
        self.inp.reset_state()
    def _forward(self, x):
        h = self.inp(x)
        y = self.out(h)
        return y

    def compute_loss(self, x, y):
        x = Variable(x.reshape(-1,1))
        y = Variable(y.reshape(-1,1))
        return F.mean_squared_error(self._forward(x), y)

    def train(self):
        cycle = 0
        while 1:
            self.reset_state()
            X,y = generate_random_vector()
            print "Current iteration: %d" % cycle
            for i in range(len(X)):
                x = X[i]
                self.optimizer.update(self.compute_loss, x,y)
            self.reset_state()

            if (cycle % 50 == 0): # Every 50'th i print the result
                X, y = generate_random_vector()
                for j in range(len(X)):
                    x = X[j]
                    print (1./self._forward(x.reshape(-1, 1)).data,1./y)
            cycle += 1


model = ToyLSTM(False)
model.train()

 A: LSTM can definitely learn this very easily. Recurrent Neural Nets are Turing complete so realistically they can compute pretty much anything you can write with code (don't bash me on this primitive explanation). 
I believe something is going wrong with your code. I've created a simple net using Keras (which I highly recommend): 
import numpy
from keras.preprocessing.text import Tokenizer
from keras.models import Model
from keras.layers import *

Data creation
x=[0]*5000
y=[0]*5000
import random
for i in range(5000):
    len = 50
    k = random.randint(5,45)
    set_of_nums = random.sample([1,2]*10000,(len-k)) + [3]*k
    random.shuffle(set_of_nums)
    x[i] = numpy.array(set_of_nums).reshape(-1,1).astype(np.float32)
    y[i] = numpy.array(1./k).astype(np.float32)
x=numpy.array(x)
y=numpy.array(y)

Network creation
inp=Input(shape=(50,1))
ls=LSTM(1)(inp)
output=Dense(1)(ls)
model=Model(inputs=[inp],outputs=[output])
model.compile(optimizer='adam', loss='mean_squared_error')

Training
model.fit(x,y,validation_split=.2)
    Train on 4000 samples, validate on 1000 samples
Epoch 1/1
4000/4000 [==============================] - 9s - loss: 0.1385 - val_loss: 0.0641

Seems like it is training well! 
