我遵循了"在Keras中构建自动编码器"教程:https://blog.keras.io/building-autoencoders-in-keras.html
第一个简单的解决方案工作正常.但是在"深度自动编码器"一节中,教程中提供的代码似乎并不完全正常.
这是我的代码(直到出现问题的地方),这只是从turorial中复制的:
from keras.layers import Input, Dense
from keras.models import Model
encoding_dim = 32
input_img = Input(shape=(784,))
encoded = Dense(128, activation='relu')(input_img)
encoded = Dense(64, activation='relu')(encoded)
encoded = Dense(32, activation='relu')(encoded) # Multiple encoding
decoded = Dense(64, activation='relu')(encoded) # and decoding layers.
decoded = Dense(128, activation='relu')(decoded)
decoded = Dense(784, activation='sigmoid')(decoded)
autoencoder = Model(input_img, decoded)
encoder = Model(input_img, encoded)
encoded_input = Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-1]
decoder = Model(encoded_input, decoder_layer(encoded_input)) # Crash happens here.
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Traceback (most recent call last):
File …Run Code Online (Sandbox Code Playgroud) 我正在创建一个小的Pandas DataFrame,并向其中添加一些应该是整数的数据。但是,即使我非常努力地将dtype显式设置为int并仅提供int值,它始终最终变成浮点数。这对我完全没有意义,而且行为甚至看起来都不完全一致。
考虑以下Python脚本:
import pandas as pd
df = pd.DataFrame(columns=["col1", "col2"]) # No dtype specified.
print(df.dtypes) # dtypes are object, since there is no information yet.
df.loc["row1", :] = int(0) # Add integer data.
print(df.dtypes) # Both columns have now become int64, as expected.
df.loc["row2", :] = int(0) # Add more integer data.
print(df.dtypes) # Both columns are now float64???
print(df) # Shows as 0.0.
# Let's try again, but be more specific.
del df
df = pd.DataFrame(columns=["col1", "col2"], dtype=int) …Run Code Online (Sandbox Code Playgroud) 首先,我创建两个ML算法并将其保存到两个单独的文件中。注意,两个模型都基于相同的数据框。feature_1和feature_2是不同组的来自同一数据集提取的特征。
import sys
from pyspark.ml.classification import RandomForestClassificationModel
trainer_1 = RandomForestClassifier(featuresCol="features_1")
trainer_2 = RandomForestClassifier(featuresCol="features_2")
model_1 = trainer_1.fit(df_training_data)
model_2 = trainer_2.fit(df_training_data)
model_1.save(sys.argv[1])
model_2.save(sys.argv[2])
Run Code Online (Sandbox Code Playgroud)
然后,当我以后要使用模型时,我必须从它们各自的路径中加载它们,并提供路径f.ex。通过sys.argv。
import sys
from pyspark.ml.classification import RandomForestClassificationModel
model_1 = RandomForestClassificationModel.load(sys.argv[1])
model_2 = RandomForestClassificationModel.load(sys.argv[2])
Run Code Online (Sandbox Code Playgroud)
我想要的是一种优雅的方法,可以将这两个模型以相同的方式保存在一起。我主要希望这样做,这样用户每次保存和加载时都不必跟踪两个单独的路径名。这两个模型紧密相连,通常将始终创建并一起使用,因此它们属于一个模型。
这是管道打算使用的东西吗?
我正在尝试使用numba.jit加速一个简单的Python循环.但似乎jit无法处理基本的数组索引和切片?我有什么办法可以做到这一点吗?我不明白如果jit无法处理基本的numpy数组,它将如何有用.
我正在逼迫nopython模式.它在对象模式下工作,但这根本不会加速代码,因此它是我需要的nopython模式.
下面的代码只是一个说明问题的例子.我的实际代码有同样的问题,但有更多的循环和迭代,以便jit非常有用.
import numpy as np
from numba import jit
n = 100
myarray = np.zeros(n)
@jit(nopython=True)
def compute(n):
for i in xrange(n):
myarray[i] += 1 # This indexing causes the error.
compute(n)
"""Sample run:
> python jit_test.py
> ...
> Failed at nopython (nopython frontend)
> Internal error at <numba.typeinfer.SetItemConstrain object at 0x7f700c89a7d0>:
> Immutable array
> File "jit_test.py", line 10
"""
Run Code Online (Sandbox Code Playgroud)
如果我尝试切片而是出现不同的错误.
# The rest of the code unchanged.
myarray[:] += 1 # This slicing causes the …Run Code Online (Sandbox Code Playgroud)