Python Keras模型预测输出是一个值介于0和1之间的数组
我正在构建一个自动编码器网络,用于查找文本中的异常值 我首先构建了一个numpy数组,将输入表示为ascii文本,但我无法将它们取回 我的输入如下所示:Python Keras模型预测输出是一个值介于0和1之间的数组,python,tensorflow,keras,deep-learning,Python,Tensorflow,Keras,Deep Learning,我正在构建一个自动编码器网络,用于查找文本中的异常值 我首先构建了一个numpy数组,将输入表示为ascii文本,但我无法将它们取回 我的输入如下所示: fab_shadow_black.9.png fab_shadow_dark.9.png fab_shadow_light.9.png fastscroller_handle_normal.xml fastscroller_handle_pressed.xml folder_fab.png ic_account_circle_grey_24dp
fab_shadow_black.9.png
fab_shadow_dark.9.png
fab_shadow_light.9.png
fastscroller_handle_normal.xml
fastscroller_handle_pressed.xml
folder_fab.png
ic_account_circle_grey_24dp.xml
ic_action_cancel_light.png
我的全部代码如下:
导入系统
从keras导入输入,模型
将matplotlib.pyplot作为plt导入
从keras.layers导入稠密
将numpy作为np导入
从pprint导入pprint
从google.colab导入驱动器
安装(“/content/drive”)
打开('/content/drive/My drive/Colab Notebooks/drawables.txt',r')作为arquivo:
dados=arquivo.read().splitlines()
def tamanho_maior_elemento(列表A):
maior=0
对于列表A中的elemento:
tamanho_elemento=len(elemento)
如果tamanho_elemento>maior:
maior=tamanho_elemento
回程票
def texto_para_ascii(列表,tamanho_maior_elemento):
lista_ascii=list()
对于列表A中的elemento:
elemento_ascii_lista=list()
elemento_com_zeros=elemento.ljust(tamanho_maior_elemento,“0”)
对于elemento_com_零中的字符:
附加(ord(字符))
lista_ascii.append(elemento_ascii_lista)
返回列表a_ascii
def ascii_para_texto(列表A):
lista_ascii=list()
对于列表A中的elemento:
elemento_ascii_lista=list()
对于elemento中的caractere:
elemento_ascii_lista.append(chr(字符))
elemento_ascii_string=”“.join(elemento_ascii_lista)
lista_ascii.append(elemento_ascii_字符串)
返回列表a_ascii
tamanho_maior_elemento=tamanho_maior_elemento(护墙板)
tamanho_lista=len(护墙板)
护墙板ascii=texto\u para\u ascii(护墙板,tamanho\u maior\u elemento)
np_dados_ascii=np.数组(dados_ascii)
tamanho_compimido=int(tamanho/5)
护墙板输入=输入(形状=(tamanho_maior_elemento,)
隐藏=密集(tamanho_compimido,activation='relu')(数据输入)
输出=密集(tamanho\u maior\u elemento,激活='relu')(隐藏)
resultado=密集(tamanho_maior_elemento,activation='sigmoid')(输出)
自动编码器=模型(输入=数据\输入,输出=结果)
编译(优化器='adam',loss='mse')
history=autoencoder.fit(np_dados_ascii,np_dados_ascii,epochs=10)
plt.绘图(历史记录[“损失”])
plt.ylabel(“损失”)
plt.xlabel(“时代”)
plt.show()
saida_predict=autoencoder.predict(np_dados_ascii)
saida_lista=saida_predict.tolist()
pprint(saida_预测)
pprint(saida_lista)
我的输入是一个numpy数组,每个字符串表示为ascii数字,右键填充为零
问题是predict的输出有很多介于0和1之间的值,我无法将它们转换回文本
array([[1. , 0.9999999 , 1. , ..., 1. , 1. ,
1. ],
[0.99992466, 1. , 1. , ..., 1. , 1. ,
1. ],
[1. , 0.99999994, 1. , ..., 1. , 1. ,
1. ],
...,
[0.9999998 , 0.9999999 , 1. , ..., 1. , 1. ,
0.9999999 ],
[1. , 0.9999998 , 1. , ..., 1. , 1. ,
1. ],
[0.9999999 , 0.99999994, 1. , ..., 1. , 1. ,
1. ]], dtype=float32)
我应该得到一个包含ascii数字的数组,就像我输入的一样,我错了什么?在你的代码中
resultado = Dense(tamanho_maior_elemento, activation='sigmoid')(output)
您使用了乙状结肠激活,这就是为什么您的预测范围在0到1之间。尝试用线性激活来改变它
resultado = Dense(tamanho_maior_elemento)(output)
对于线性激活,您不需要在激活中分配任何内容,因为它提到默认值是线性激活。在您的代码中
resultado=Dense(tamanho\u maior\u elemento,activation='sigmoid')(输出)
您使用了sigmoid激活,这就是为什么预测范围在0到1之间。尝试用线性激活来更改它。@kruxx我找不到激活“”中应该放什么要使用线性激活,我应该放什么?你不需要在激活中分配任何东西,因为它提到默认值是线性激活。@kruxx工作了!如果你提交答案,我将标记为正确。但是现在我不知道如何解释这个函数给我的结果。我问了另一个问题:
resultado = Dense(tamanho_maior_elemento)(output)