为什么我的训练后的权重显示为这种奇怪的格式?

我目前正在学习逻辑回归,并且在尝试从训练后的权重中绘制一条线时遇到了困难。我期望得到一个包含3个值的数组,但在打印权重进行检查时,我得到的格式每次都不同,但格式相同:

[array([[ 0.42433906], [-0.67847246]], dtype=float32) array([-0.06681705], dtype=float32)]

我的问题是,为什么权重是以这种两个数组的格式呈现,而不是一个长度为3的数组?以及如何解释这些权重,以便我能够绘制分隔线?

这是我的代码:

from tensorflow import kerasfrom keras.models import Sequentialfrom keras.layers import Densefrom keras.regularizers import L1L2import randomimport numpy as np# return the array data of shape (m, 2) and the array labels of shape (m, 1)def get_random_data(w, b, mu, sigma, m): # slope, y-intercept, mean of the data, standard deviation, size of arrays  data = np.empty((m, 2))  labels = np.empty((m, 1))  # fill the arrays with random data  for i in range(m):    c = (random.random() > 0.5) # 0 with probability 1/2 and 1 with probability 1/2    n = random.normalvariate(mu, sigma) # noise using normal distribution    x_1 = random.random() # uniform distribution on [0, 1)    x_2 = w * x_1 + b + (-1)**c * n    labels[i] = c    data[i][0] = x_1    data[i][1] = x_2  # the train set is the first 80% of our data, and the test set is the following 20%  train_length = int(round(m * 0.8, 1))   train_data = np.empty((train_length, 2))  train_labels = np.empty((train_length, 1))  test_data = np.empty((m - train_length, 2))  test_labels = np.empty((m - train_length, 1))  for i in range(train_length):    train_data[i] = data[i]    train_labels[i] = labels[i]  for i in range(train_length, m):    test_data[i - train_length] = data[i]    test_labels[i - train_length] = labels[i]  return (train_data, train_labels), (test_data, test_labels)(train_data, train_labels), (test_data, test_labels) = get_random_data(2,3,100,100,200)model = Sequential()model.add(Dense(train_labels.shape[1],                activation='sigmoid',                kernel_regularizer=L1L2(l1=0.0, l2=0.1),                input_dim=(train_data.shape[1]))) model.compile(optimizer='sgd',              loss='binary_crossentropy',              metrics=['accuracy'])model.fit(train_data, train_labels, epochs=100, validation_data=(test_data,test_labels))weights = np.asarray(model.get_weights())print("the weights are " , weights)

回答:

数组的第一个索引显示的是系数的权重,第二个数组显示的是偏置项。

因此,您可以得到如下方程:

h(x) = 0.42433906x1 + -0.67847246x2 + -0.06681705

逻辑回归会采用这个方程,并应用 sigmoid 函数将结果压缩在0到1之间。

所以,如果您想绘制一条线的方程,您可以按照我上面解释的方式使用返回的权重来实现。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注