2017-07-12 15 views
7

Tôi muốn triển khai LIME trên mô hình được tạo bằng cách sử dụng h2o (học sâu) trong R. Để sử dụng dữ liệu trong mô hình, tôi đã tạo h2oFrames và chuyển nó trở lại dataframe trước khi sử dụng Hàm LIME (hàm vôi, vì hàm giải thích của LIME không thể nhận ra h2oFrame). Ở đây tôi có thể chạy hàmThực hiện LIME trên mô hình h2o trong R

Bước tiếp theo là sử dụng hàm giải nghĩa trên dữ liệu thử nghiệm để tạo giải thích. Ở đây R ném một lỗi khi sử dụng một khung dữ liệu cũng như h2oFrame.

Đây là lỗi phát sinh khi sử dụng một dataframe:

Error in chk.H2OFrame(x) : must be an H2OFrame 

Đây là lỗi phát sinh khi sử dụng một h2oframe:

Error in UseMethod("permute_cases") : 
    no applicable method for 'permute_cases' applied to an object of class "H2OFrame" 
if(!require(pacman)) install.packages("pacman") 
pacman::p_load(h2o, lime, data.table, e1071) 

data(iris) 
h2o.init(nthreads = -1) 
h2o.no_progress() 

# Split up the data set 
iris <- as.h2o(iris) 

split <- h2o.splitFrame(iris, c(0.6, 0.2), seed = 1234) 
iris_train <- h2o.assign(split[[1]], "train") # 60% 
iris_valid <- h2o.assign(split[[2]], "valid") # 20% 
iris_test <- h2o.assign(split[[3]], "test") # 20% 


output <- 'Species' 
input <- setdiff(names(iris),output) 


model_dl_1 <- h2o.deeplearning(
    model_id = "dl_1", 
    training_frame = iris_train, 
    validation_frame = iris_valid, 
    x = input, 
    y = output, 
    hidden = c(32, 32, 32), 
    epochs = 10, # hopefully converges earlier... 
    score_validation_samples = 10000, 
    stopping_rounds = 5, 
    stopping_tolerance = 0.01 
) 

pred1 <- h2o.predict(model_dl_1, iris_test) 
list(dimension = dim(pred1), pred1$predict) 

#convert to df from h2ofdataframe 

train_org<-as.data.frame(iris_train) 
#converting train h2oframe to dataframe 
sapply(train_org,class) #checking the class of train_org 
test_df <- as.data.frame(iris_test) 
#converting test data h2oFrame to dataframe 
test_sample <- test_df[1:1,] 

#works 
#lime is used to get explain on the train data 
explain <- lime(train_org, model_dl_1, bin_continuous = FALSE, n_bins = 
        5, n_permutations = 1000) 


# Explain new observation 
explanation <- explain(test_sample, n_labels = 1, n_features = 1) 
h2o.shutdown(prompt=F) 

Can bất cứ ai xin se giúp tôi tìm giải pháp hoặc cách sử dụng hàm giải thích của LIME với dataFrame phù hợp

+0

Vui lòng cung cấp ví dụ về mã có thể tái sản xuất đầy đủ cũng như thông tin phiên bản về gói vôi và h2o R. –

+1

Bạn cần cập nhật mã trong bài đăng của mình để nó có thể tái sản xuất - nó có thể là bất kỳ tập dữ liệu nào (iris sẽ ổn). Vui lòng xem nguyên tắc Ngăn xếp ngăn xếp về MCVE tại đây: https://stackoverflow.com/help/mcve Nếu tôi không thể sao chép/dán mã của bạn để giúp bạn gỡ lỗi mã, thì đó không phải là MCVE. –

+1

@ErinLeDell, cảm ơn bạn đã phản hồi, tôi sẽ thực hiện các thay đổi. – gattaca

Trả lời

4

Gói lime dưới mui xe sử dụng hai chức năng, predict_model()model_type() mà bạn cần phải thiết lập cho bất kỳ mô hình nào hiện không được hỗ trợ.

Ví dụ cụ thể của bạn, dưới đây là những gì bạn cần làm.

Bước 1: Thiết lập chức năng chung model_type cho các kiểu của lớp H2OMultinomialModel. Tất cả những gì bạn làm ở đây là cho biết lime loại mô hình nào bạn muốn nó thực hiện như "phân loại" hoặc "hồi quy".

model_type.H2OMultinomialModel <- function(x, ...) { 
    # Function tells lime() what model type we are dealing with 
    # 'classification', 'regression', 'survival', 'clustering', 'multilabel', etc 
    # 
    # x is our h2o model 

    return("classification") 

} 

Bước 2: Thiết lập một predict_model chức năng chung cho các mô hình của lớp H2OMultinomialModel. Chìa khóa ở đây là sự hiểu biết rằng đối với vôi để làm việc nó cần xác suất phân loại hơn là dự đoán (điều này đã cho tôi một chút thời gian để tìm ra và nó phải đối phó với một biến lime:::output_type(explaination)).

predict_model.H2OMultinomialModel <- function(x, newdata, type, ...) { 
    # Function performs prediction and returns dataframe with Response 
    # 
    # x is h2o model 
    # newdata is data frame 
    # type is only setup for data frame 

    pred <- h2o.predict(x, as.h2o(newdata)) 

    # return classification probabilities only 
    return(as.data.frame(pred[,-1])) 

} 

Khi bạn đặt các chức năng này đúng cách, bạn có thể chạy tập lệnh lime.

# Lime is used to get explain on the train data 
explainer <- lime(train_org, model_dl_1, bin_continuous = FALSE, n_bins = 5, n_permutations = 1000) 

# Explain new observation 
explanation <- explain(test_sample, explainer, n_labels = 1, n_features = 1) 
+0

Lưu ý rằng gói 'lime' đã được cập nhật để tích hợp' h2o'. Bạn có thể cần tải xuống phiên bản GitHub tại đây: https://github.com/thomasp85/lime –

Các vấn đề liên quan