Using Theano: MissingInputError

Hi,
this is my first post, i hope this is the right place where post it.

this is a semplification of the original code that i’m trying to fix. I have invented a problem that reflects what the original code does and which gives me the same error but is written in a minimal form:

import theano
import theano.tensor as T
import numpy as np
import copy
theano.config.exception_verbosity = 'high'


class Test(object):
    def __init__(self):
        self.rate=0.01
    def start(self, x, y):
        W_val=40.89
        W=theano.shared(value=W_val, borrow=True)
        z=T.mean(x*W/y)
        gz=T.grad(z, W)
        updates=[(W, W-self.rate*gz)]

        fz=theano.function([], z, updates=updates)
        for i in range(100):
            out=fz()    
        self.out=out    
        return out

 x_set=np.random.rand(10)
 y_set=np.random.randint(low=0, high=5, size=10, dtype=int)
 batch_size=2

 x = T.dvector('x')
 y = T.ivector('y')
 index = T.lscalar()

test = Test()
cost=test.start(x,y)

train = theano.function(
    inputs=[index],
    outputs=cost,
    givens={
        x: x_set[index * batch_size: (index + 1) * batch_size],
        y: y_set[index * batch_size: (index + 1) * batch_size]
   }
)

for i in range(5):
    result=train(i)
    print(result)

This is the TraceBack:

Traceback (most recent call last):
  File "functions.py", line 33, in <module>
    cost=test.start(x,y)
  File "functions.py", line 18, in start
    fz=theano.function([], z, updates=updates)
  File "C:\Program Files\Anaconda3\lib\site-packages\theano\compile\function.py", line 320, in function
    output_keys=output_keys)
  File "C:\Program Files\Anaconda3\lib\site-packages\theano\compile\pfunc.py", line 479, in pfunc
    output_keys=output_keys)
  File "C:\Program Files\Anaconda3\lib\site-packages\theano\compile\function_module.py", line 1776, in orig_function
    output_keys=output_keys).create(
  File "C:\Program Files\Anaconda3\lib\site-packages\theano\compile\function_module.py", line 1428, in __init__
    accept_inplace)
  File "C:\Program Files\Anaconda3\lib\site-packages\theano\compile\function_module.py", line 177, in std_fgraph
    update_mapping=update_mapping)
  File "C:\Program Files\Anaconda3\lib\site-packages\theano\gof\fg.py", line 171, in __init__
    self.__import_r__(output, reason="init")
  File "C:\Program Files\Anaconda3\lib\site-packages\theano\gof\fg.py", line 360, in __import_r__
    self.__import__(variable.owner, reason=reason)
  File "C:\Program Files\Anaconda3\lib\site-packages\theano\gof\fg.py", line 465, in __import__
    detailed_err_msg)
theano.gof.fg.MissingInputError: A variable that is an input to the graph was neither provided as an input to the function nor given a value. A chain of variables leading from this input to an output is [x, Elemwise{mul,no_inplace}.0, Elemwise{true_div,no_inplace}.0, Sum{acc_dtype=float64}.0, mean]. This chain may not be unique
Backtrace when the variable is created:
  File "functions.py", line 28, in <module>
    x = T.dvector('x')

I do not know where to slam my head again… Thanks