LoginSignup
2
1

More than 3 years have passed since last update.

Pythonエラー対応メモ: "...does not support argument 0 of type float..."

Posted at

Python / 機械学習の初心者です。
データ型に対するエラー対応のメモを残します。

発生した状況

pythonのプログラム内で、以下のような計算をした際にエラーが発生した。

import numpy as np

def sigmoid(x)
  return (1 / (1 + np.exp(-x)))

hoge = sigmoid(3) #ここでエラー発生

TypeError: loop of ufunc does not support argument 0 of type float which has no callable exp method

判明した原因

  • どうやら、numpyのライブラリの計算にあてる引数に、int型のデータが含まれているとエラーが出るらしい(参考欄のGitHub URL参照)。
  • 引数への計算処理前にfloat型へとデータ型変更をすると良い。

解決策

  • sigmoid(x)のxのデータ型をfloatであると明示することで解決した。
def sigmoid(x)
  x = x.float()
  return (1 / (1 + np.exp(-x)))

hoge = sigmoid(3) # -> エラーは起こらず正しく計算できた

参考

補言

  • ディープラーニング作成時、シグモイド関数を利用するタイミングでこのエラーが発生しました。引数のxには配列が入るのですが、配列の各要素がint型をしていたために今回の問題を起こしたようです。
  • データ型を宣言しなくてもある程度動くのがPythonの特徴の1つだと改めて認識し、データ型を明示した計算を心がけたほうが良いということなのだろうと思いました。

(以上)

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1