conversion from numpy to pytorch

Asked 2 years ago, Updated 2 years ago, 131 views

I'm working on a prediction program for machine learning, and I'm working on two models: the one written in tensorflow and the one written in pytorch.
I'd like to convert a prediction model written on a tensorflow basis to a pytorch-based model, but it doesn't work. How should I deal with it?Please let me know.

process flow:

print(data)→[1,4,5,6,7,3,1,3],[1,2,3,4,5,6,7,8].

Error Messages

response=np.argmax(np.bincount(temp))# numpy format
File "<_array_function__internals>", line 6, inbound
only one element sensors can be converted to Python scalars

Code

 if__name__=='__main__':

    myo.init(bin_path=r'C:\Users\name\Desktop\myo-sdk-win-0.9.0\bin')
    HUB = myo.Hub()
    model.eval()
    listener=MyListener()
    start = time.time()
    temp=[]#Create list
    with HUB.run_in_background(listener.on_event):
        while True:
            data=listener.get_emg_data()#Acquired myoelectric signal
            if time.time()-start>=10:
                response=np.argmax(np.bincount(temp))# numpy format
                response=torch.tensor(response)# Convert to tensor
                print(response)
                print("Predicted gesture: {0}".format(response))
                temp = [ ] 
                start = time.time()

            iflen(data)>0:#len(data)=8
                tmp = [ ]
                for vin listener.get_emg_data():
                    tmp.append(v[1])
                tmp=list(np.stack(tmp).flatten())
                tmp=torch.tensor(tmp)# Convert to tensor type (list contents)
                print(tmp)
                iflen(tmp)>=64:
                    pred=model(tmp)
                    # pred=torch.mean(_,predicted,feed_dict={x:np.array([tmp])})
                    # pred=sess.run(y_pred_cls, feed_dict={x:np.array([tmp])})
                    print(pred)
                    temp.append(pred[0])
            sleep(0.01)

python numpy pytorch

2022-09-29 21:58

1 Answers

I don't know exactly what's going on, but
response=np.argmax(np.bincount(temp))#numpy format
I thought there might be something wrong with the temp type.

I just moved it in my environment, and it seems to pass if it is as follows.
np.bincount([1,2,3,4,1,2,3,4]))
np.bincount(np.array([1,2,3,4,1,2,3,4]))
What is the shape of temp now?In the comments on January 6th,

The data looks like the following. [(1609922196940975, [-2,0,2,-1,2,0,1,1], (1609922196940975, [2,0,-1,0,0,-2,1]], ………

Does this mean temp?In my environment

temp=[(1609922196940975, [-2,0,2,-1,2,0,1,1], (1609922196940975, [2,0,-1,0,0,-2,1]]]
np.argmax(np.bincount(temp))

When I ran , the error that occurred was ValueError:object too deep for desired array, so I thought it was different...
Why don't you try converting the temp type or entering a specific value to find a model that might pass?


2022-09-29 21:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.