TypeError: where() received an invalid combination of arguments

Question:

I want to make a judgment on the value predicted by the neural network. If it is greater than 0.5, it is 1, and if it is less than 0.5, it is 0.When I ran my model,I met this problem.

Input In [73], in create_model(n_inputs)
     45 a = torch.ones(n_inputs,1)
     46 b = torch.zeros(n_inputs,1)
---> 48 indicator_output = torch.where(indicator_probability>0.5, a, b)

TypeError: where() received an invalid combination of arguments - got (Tensor, Tensor, Tensor), but expected one of:
 * (Tensor condition)
 * (Tensor condition, Tensor input, Tensor other, *, Tensor out)
 * (Tensor condition, Number self, Tensor other)
      didn't match because some of the arguments have invalid types: (Tensor, Tensor, Tensor)
 * (Tensor condition, Tensor input, Number other)
      didn't match because some of the arguments have invalid types: (Tensor, Tensor, Tensor)
 * (Tensor condition, Number self, Number other)
      didn't match because some of the arguments have invalid types: (Tensor, Tensor, Tensor)

My code:

import torch
import numpy as np
import pandas as pd

import tensorflow as tf

tf.get_logger().setLevel('ERROR')
import tensorflow.python.keras.backend as k
from tensorflow.python.keras import layers,Model,callbacks,Sequential
from tensorflow.python.keras.callbacks import EarlyStopping
from tensorflow.python.keras.layers import GRU,LSTM,Layer,LayerNormalization,Input,Conv1D,Embedding,Flatten,RepeatVector,GlobalAveragePooling1D,Masking,concatenate,TimeDistributed,Dense,Dropout
from tensorflow.python.keras.layers.core import Lambda
from tensorflow.python.keras import optimizers
from tensorflow.python.keras.optimizers import SGD
from tensorflow.python.keras.optimizers import Adam,rmsprop
from tensorflow.python.keras.losses import categorical_crossentropy
from tensorflow.python.keras.models import load_model
from tensorflow.python.keras.initializers import Constant
tf.compat.v1.disable_eager_execution()
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
print('Tensorflow version: {}'.format(tf.__version__))


def create_model(n_inputs):

  all_inputs = Input(shape=(n_inputs, 2),name = "all_inputs")
    
  dense1 = TimeDistributed(Dense(32, activation='relu'))
  dropout = TimeDistributed(Dropout(0.2))
  dense2 = TimeDistributed(Dense(1, activation='sigmoid'))  

  indicator_probability = dense2(dropout(dense1(outputs)))
  a = torch.ones(n_inputs,1)
  b = torch.zeros(n_inputs,1)

  indicator_output = torch.where(indicator_probability>0.5, a, b)
  
  model = Model(inputs=all_inputs, outputs=indicator_output)

  return model

prediction_model = create_model(n_inputs=11)
prediction_model.compile(optimizer='adam')

I didn’t know why this problem happended.Hope to get a solution

Asked By: Theresa Liu

||

Answers:

enter image description here The first tensor is an impostor!

You used a tensorflow.python.keras.layers.Dense to create the dense layer, so it returns a tensorflow.python.framework.ops.Tensor. Only PyTorch tensors transform into TensorCondition when used in a condition. Therefore:

  dense2 = TimeDistributed(Dense(1, activation='sigmoid')) 
  indicator_probability = dense2(dropout(dense1(outputs)))
  torch.where(indicator_probability>0.5, a, b)

indicator_probability>0.5 dit did not turn into a TensorCondition.

where() expected (TensorCondition, Tensor, Tensor) but got instead a (TensorFlow Tensor, PyTroch Tensor, Pytorch Tensor), which he proceeded to display as (Tensor, Tensor, Tensor), hiding the datatype of the first attribute.

Answered By: Florian Fasmeyer

If I understand correctly, you just want to check where indicator_probability>0.5 then it’s 1, else, 0. Then, you just need a simple line:

indicator_output = torch.where(indicator_probability>0.5, 1.0, 0.0)
Answered By: Minh-Long Luu