[英]TypeError: float() argument must be a string or a number, not 'dict_values',
I was trying to plot the loss for my model, so coded like below, but I am getting below error:我试图 plot 我的 model 的损失,所以编码如下,但我得到以下错误:
TypeError: float() argument must be a string or a number, not 'dict_values'
TypeError:float() 参数必须是字符串或数字,而不是 'dict_values'
code:代码:
def fit(self, X, Y, epochs=1, learning_rate=1, initialise=True, display_loss=False):
#initialise w,b
if initialise:
self.w = np.random.randn(1,X.shape[1])
self.b = 0
if display_loss:
loss = {}
for i in tqdm_notebook(range(epochs), total=epochs, unit='epoch'):
dw = 0
db = 0
for x,y in zip(X,Y,):
dw += self.grad_w(x,y)
db += self.grad_b(x,y)
self.w -= learning_rate * dw
self.b -= learning_rate * db
if display_loss:
Y_pred = self.sigmoid(self.perceptron(X))
loss[i] = mean_squared_error(Y_pred, Y)
if display_loss:
plt.plot(loss.values())
plt.xlabel('Epochs')
plt.ylabel('Mean Squared Error')
plt.show()
def predict(self, X):
Y_pred = []
for x in X:
y_pred = self.sigmoid(self.perceptron(x))
Y_pred.append(y_pred)
return np.array(Y_pred)
sn = SigmoidNeuron()
sn.fit(X_scaled_train, Y_scaled_train, epochs=1000, learning_rate=0.01, display_loss=True)
TypeError: float() argument must be a string or a number, not 'dict_values'
TypeError:float() 参数必须是字符串或数字,而不是 'dict_values'
I got your error.
我得到了你的错误。 This error occur if you are plotting the
loss.values as it is... which you shouldn't do it.
如果您按原样绘制loss.values ,则会发生此错误......您不应该这样做。
which creates an array of type object and puts the dict_values object inside:
它创建了一个 object 类型的数组,并将 dict_values object 放入其中:
plt.plot(loss.values())
instead of this you should use
plt.plot(loss.values())
而不是这个你应该使用
>
plt.plot(np.array(list(loss.values())).astype(float))
>
plt.plot(np.array(list(loss.values())).astype(float))
so your code should look like this:所以你的代码应该是这样的:
if display_loss:
plt.plot(np.array(list(loss.values())).astype(float))
plt.xlabel('Epochs')
plt.ylabel('Mean Squared Error')
plt.show()
so when you run the fit function or
invoke that function with this
sn.fit(X_scaled_train, Y_scaled_train, epochs=2000, learning_rate=0.015, display_loss=True)
因此,当您运行拟合 function或使用此
sn.fit(X_scaled_train, Y_scaled_train, epochs=2000, learning_rate=0.015, display_loss=True)
调用 function时
then you will get the loss plotting image as loss_plotting .然后你会得到损失绘图图像loss_plotting 。
I took reference of this snippet Float Arguments and dict_values with NumPy我参考了这个片段Float Arguments 和 dict_values 与 NumPy
Hope you get it...!希望你能得到它...!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.