micrograd icon indicating copy to clipboard operation
micrograd copied to clipboard

Homework Assignment Error with softmax activation function

Open Athe-kunal opened this issue 3 years ago • 1 comments

Hi @karpathy I was solving the assignment as mentioned in the YouTube video. In the Softmax function, I was getting the following error TypeError: unsupported operand type(s) for +: 'int' and 'Value'

This is the line where I am getting the error

def softmax(logits):
  counts = [logit.exp() for logit in logits]
  denominator = sum(counts) #Here I am getting the Typeerror
  out = [c / denominator for c in counts]
  return out

And, my add function in Value Class is the following

def __add__(self, other): # exactly as in the video
    other = other if isinstance(other, Value) else Value(other)
    out = Value(self.data + other.data, (self, other), '+')
    
    def _backward():
      self.grad += 1.0 * out.grad
      other.grad += 1.0 * out.grad
    out._backward = _backward
    
    return out

So my query is on the sum of list function. It is probably similar to counts[i].add(counts[i+1]) and then we keep on adding to the result till the end of the list. So this add function should work well. But I am not sure why it is not working, am I missing something? Thanks in advance

Athe-kunal avatar Nov 20 '22 07:11 Athe-kunal

as per the video the reason why your solution does not work is because the + operator is being invoked on the integer value first, and since it cannot be found there, it looks for the __radd__ on the Value object, which you have not defined. __radd__ stands for reverse add

djmittens avatar Dec 04 '22 11:12 djmittens