IndexError: fail to coerce slice entry of type tensorvariable to integer

2024/9/16 18:11:34

I run "ipython debugf.py" and it gave me error message as below

IndexError                                Traceback (most recent call last)  
/home/ml/debugf.py in <module>()  8 fff = theano.function(inputs=[index],  9                         outputs=cost,  
---> 10                         givens={x: train_set_x[index: index+1]})  IndexError: failed to coerce slice entry of type TensorVariable to integer"   

I search the forum and no luck, is there someone can help ? thanks!
debugf.py :

import theano.tensor as T    
import theano    
import numpy    
index =T.lscalar()    
x=T.dmatrix()    
cost=x +index    
train_set_x=numpy.arange(100).reshape([20,5])    
fff=theano.function(inputs=[index],    outputs=cost,    givens={x:train_set_x[index: index+1]})   #<--- Error here    
Answer

Change train_set_x variable to theano.shared variable, and the code is OK. I dont know the reason, but it works! Hope this post can help others. The correct code is as below

import theano.tensor as T    
import theano    
import numpy    
index =T.lscalar()    
x=T.dmatrix()    
cost=x +index    
train_set_x=numpy.arange(100.).reshape([20,5]) #<--- change to float,#because shared must be floatX type#change to shared variable
shared_x = theano.shared(train_set_x)fff=theano.function(inputs=[index],    outputs=cost,    givens={x:shared_x[index: index+1]})  #<----change to shared_x
https://en.xdnf.cn/q/73317.html

Related Q&A

How to detect lines in noisy line images?

I generate noisy images with certain lines in them, like this one:Im trying to detect the lines using OpenCV, but something is going wrong.Heres my code so far, including the code to generate the noisy…

How can I connect a StringVar to a Text widget in Python/Tkinter?

Basically, I want the body of a Text widget to change when a StringVar does.

python csv writer is adding quotes when not needed

I am having issues with writing json objects to a file using csv writer, the json objects seem to have multiple double quotes around them thus causing the json objects to become invalid, here is the re…

How to install google.cloud automl_v1beta1 for python using anaconda?

Google Cloud AutoML has python example code for detection, but I have error when importing these modulesfrom google.cloud import automl_v1beta1 from google.cloud.automl_v1beta1.proto import service_pb2…

Python3.8 - FastAPI and Serverless (AWS Lambda) - Unable to process files sent to api endpoint

Ive been using FastAPI with Serverless through AWS Lambda functions for a couple of months now and it works perfectly.Im creating a new api endpoint which requires one file to be sent.It works perfectl…

How to create a function for recursively generating iterating functions

I currently have a bit of Python code that looks like this:for set_k in data:for tup_j in set_k:for tup_l in tup_j:The problem is, Id like the number of nested for statements to differ based on user in…

How to get molecular weight of a compound in python?

User inputs a formula, for example: C12H2COOHWe have to calculate its molecular weight given that C = 12.01, H = 1.008 and O = 16. We were told to be careful of elements with double digits after it and…

How to install Numpy and Pandas for AWS Lambdas?

Problem: I wanted to use Numpy and Pandas in my AWS lambda function. I am working on Windows 10 with PyCharm. My function compiles and works fine on local machine, however, as soon as package it up and…

vim highlighting everything in red

I added a print line to a python script while the script was executing, and now all the text is highlighted in red when I open the file. Opening and closing the file doesnt get rid of it. Opening a sec…

Using python modules in node.js

Is it possible to create a glue that makes it possible for python modules (more specifically, library bindings) to be used in node.js? Some of the data structures can be directly mapped to V8 objects …