小编Ken*_*han的帖子

Matplotlib图形图像到base64

问题:需要将matplotlib的图形图像转换为base64图像

当前解决方案:将matplot图像保存在缓存文件夹中,并使用read()方法读取它,然后转换为base64

新问题:烦恼:需要一个解决方法,所以我不需要将图形保存为任何文件夹中的图像.我想在内存中使用图像.执行不必要的I/O是一种不好的做法.

def save_single_graphic_data(data, y_label="Loss", x_label="Epochs", save_as="data.png"):
    total_epochs = len(data)
    plt.figure()
    plt.clf()

    plt.plot(total_epochs, data)

    ax = plt.gca()
    ax.ticklabel_format(useOffset=False)

    plt.ylabel(y_label)
    plt.xlabel(x_label)

    if save_as is not None:
        plt.savefig(save_as)

    plt.savefig("cache/cached1.png")

    cached_img = open("cache/cached1.png")

    cached_img_b64 = base64.b64encode(cached_img.read())

    os.remove("cache/cached1.png")

    return cached_img_b64
Run Code Online (Sandbox Code Playgroud)

python matplotlib python-2.7

9
推荐指数
3
解决办法
3756
查看次数

如何在theano上实现加权二元交叉熵?

如何在theano上实现加权二元交叉熵?

我的卷积神经网络只能预测0~1(sigmoid).

我想以这种方式惩罚我的预测:

成本表

基本上,我想在模型预测为0时惩罚更多,但事实是1.

问题:如何使用theano和lasagne创建此加权二进制CrossEntropy函数?

我试过这个

prediction = lasagne.layers.get_output(model)


import theano.tensor as T
def weighted_crossentropy(predictions, targets):

    # Copy the tensor
    tgt = targets.copy("tgt")

    # Make it a vector
    # tgt = tgt.flatten()
    # tgt = tgt.reshape(3000)
    # tgt = tgt.dimshuffle(1,0)

    newshape = (T.shape(tgt)[0])
    tgt = T.reshape(tgt, newshape)

   #Process it so [index] < 0.5 = 0 , and [index] >= 0.5 = 1


    # Make it an integer.
    tgt = T.cast(tgt, 'int32')


    weights_per_label = theano.shared(lasagne.utils.floatX([0.2, 0.4])) …
Run Code Online (Sandbox Code Playgroud)

python theano keras lasagne cross-entropy

8
推荐指数
1
解决办法
1845
查看次数

Gradle badass-runtime-plugin和ProGuard Gradle插件

jPackage之前如何运行proguard?

介绍

我使用gradle插件在JavaFx中开发了一个应用程序,并使用gradle插件将其与jPackager打包在一起。

我使用的主要插件是:

id 'org.openjfx.javafxplugin' version '0.0.8'
id 'org.beryx.runtime' version '1.7.0'
id "com.github.johnrengelman.shadow" version "5.1.0"
Run Code Online (Sandbox Code Playgroud)

我当前的gradle版本是:gradle-5.6.2-all

问题描述

我如何使用proguard,以便在jPackage执行工作之前对代码进行混淆和优化?

我可以运行proguard任务,但是当我运行jPackage时,代码不会混淆!

Ive找到了较早的gradle版本的教程(Tutorial),但是我不确定如何将其与当前插件混合使用。我已经尝试了一些代码片段,但是它们都无法构建,并且我不想用一堆无法正常工作的代码来使这个话题变得混乱。

我当前的工作build.gradle

// 1. Include proguard dependency
buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'net.sf.proguard:proguard-gradle:6.2.0'
    }
}

plugins {
    id 'java'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.8'
    id 'org.beryx.runtime' version '1.7.0'
    id "com.github.johnrengelman.shadow" version "5.1.0"

}


dependencies {
    compile "org.controlsfx:controlsfx:11.0.0"
    compile "eu.hansolo:tilesfx:11.13"
    compile "com.jfoenix:jfoenix:9.0.9"
    compile "org.apache.httpcomponents:httpclient:4.5.9"
    compile "org.json:json:20180813"
    compile "mysql:mysql-connector-java:8.0.17"
    compile "org.jasypt:jasypt:1.9.3"
    compile "com.mchange:c3p0:0.9.5.4" …
Run Code Online (Sandbox Code Playgroud)

java upgrade proguard gradle build.gradle

8
推荐指数
1
解决办法
210
查看次数

XgBoost:y中填充最少的类只有1个成员,这个成员太少了

我在sklearn上使用Xgboost实现了一个讨人喜欢的比赛.但是,我收到此"警告"消息:

$ python Script1.py /home/sky/private/virtualenv15.0.1dev/myVE/local/lib/python2.7/site-packages/sklearn/cross_validation.py:516:

警告:y中填充最少的类只有1个成员,这个成员太少了.任何类的最小标签数不能少于n_folds = 3.%(min_labels,self.n_folds)),警告)

根据stackoverflow的另一个问题:"检查每个类至少有3个样本能够用k == 3进行StratifiedKFold交叉验证(我认为这是GridSearchCV用于分类的默认CV)."

好吧,我每班至少有3个样本.

所以我的问题是:

a)有哪些替代方案?

b)为什么我不能使用交叉验证?

c)我可以使用什么?

...
param_test1 = {
    'max_depth': range(3, 10, 2),
    'min_child_weight': range(1, 6, 2)
}

grid_search = GridSearchCV(

estimator=
XGBClassifier(
    learning_rate=0.1,
    n_estimators=3000,
    max_depth=15,
    min_child_weight=1,
    gamma=0,
    subsample=0.8,
    colsample_bytree=0.8,
    objective='multi:softmax',
    nthread=42,
    scale_pos_weight=1,
    seed=27),

    param_grid=param_test1, scoring='roc_auc', n_jobs=42, iid=False, cv=None, verbose=1)
...

grid_search.fit(train_x, place_id)
Run Code Online (Sandbox Code Playgroud)

参考文献:

使用scikit-learn进行一次性学习

在scikit-learn中使用带有多项式内核的支持向量分类器

python scikit-learn cross-validation xgboost

6
推荐指数
1
解决办法
2万
查看次数

为什么卷积神经网络中的卷积滤波器会翻转?

我不明白为什么在使用卷积神经网络时需要翻转过滤器。

根据千层面文件,

翻转过滤器:布尔(默认值:真)

是否在将过滤器滑过输入之前翻转过滤器,执行卷积(这是默认设置),或者不翻转它们并执行相关。请注意,对于 Lasagne 中的其他一些卷积层,翻转会产生开销,并且默认情况下处于禁用状态 - 使用从另一层学习的权重时请查看文档。

这意味着什么?在任何神经网络书籍中,我从未读过关于卷积时翻转滤波器的内容。请有人澄清一下好吗?

convolution neural-network theano conv-neural-network lasagne

6
推荐指数
1
解决办法
5175
查看次数

'Fork' git 存储库作为 gradle 中的依赖项

几个小时前我做了一个话题,将我带到了一个公共存储库:https : //github.com/biezhi/webp-io

但是,我必须更新使用的库 cwebp 并对代码进行更改。这是我的第一个叉子。

我的叉子位于这里:https : //github.com/KenobySky/webp-io

maven {url "https://jitpack.io"}
...
compile 'com.github.KenobySky:webp-io:master'
Run Code Online (Sandbox Code Playgroud)

问题: 我试图将这个“fork”git 存储库声明为 gradle 中的依赖项,但我在下面收到此错误,我该怎么办?


Execution failed for task ':compileJava'.
> Could not resolve all files for configuration ':compileClasspath'.
  > Could not find com.github.KenobySky:webp-io:master.
    Searched in the following locations:
      - https://repo.maven.apache.org/maven2/com/github/KenobySky/webp-io/master/webp-io-master.pom
    If the artifact you are trying to retrieve can be found in the repository but without metadata in 'Maven POM' format, you need to adjust the 'metadataSources { ... }' of the …
Run Code Online (Sandbox Code Playgroud)

java gradle build.gradle jitpack

6
推荐指数
1
解决办法
560
查看次数

Keras - 文本分类 - LSTM - 如何输入文本?

我试图了解如何使用LSTM对我拥有的某个数据集进行分类.

我研究并发现了这个keras和imdb的例子:https: //github.com/fchollet/keras/blob/master/examples/imdb_lstm.py

但是,我对如何处理数据集进行输入感到困惑.

我知道keras有预处理文本方法,但我不确定使用哪种方法.

x包含带有文本的n行,y通过快乐/悲伤对文本进行分类.基本上,1.0意味着100%快乐,0.0意味着完全悲伤.数字可能会有所不同,例如0.25~~等等.

所以我的问题是,我如何正确输入x和y?我必须用文字袋吗?任何提示表示赞赏!

我在下面编码,但我一直得到同样的错误:

#('Bad input argument to theano function with name ... at index 1(0-based)', 
'could not convert string to float: negative')
Run Code Online (Sandbox Code Playgroud)
import keras.preprocessing.text
import numpy as np

np.random.seed(1337)  # for reproducibility

from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.layers.embeddings import Embedding
from keras.layers.recurrent import LSTM

print('Loading data...')
import pandas

thedata = pandas.read_csv("dataset/text.csv", sep=', ', delimiter=',', header='infer', names=None)

x = thedata['text']
y = thedata['sentiment']

x = …
Run Code Online (Sandbox Code Playgroud)

theano lstm keras lasagne

5
推荐指数
1
解决办法
6929
查看次数

在Matplotlib中绘制多个直方图 - 颜色或并排条形图

问题:在Matplotlib中绘制多个直方图时,我无法区分绘图与另一个绘图

图像问题:**问题 **次要问题:部分左侧标签"计数"不在图像范围内.为什么?

描述

我想绘制3个不同组的直方图.每组都是一个0和1的数组.我想要每个的直方图,所以我可以检测数据集上的不平衡.

我让它们分开绘制,但我想要一起绘制它们的图形.

可以并排显示不同的图形,或者我甚至用谷歌搜索将其绘制为3D,但我不知道在图形上"阅读"或"查看"并理解它是多么容易.

现在,我想在同一图形的每一侧绘制[train],[validation]和[test]条形图,如下所示:

我想要这样

PS:我的谷歌搜索没有返回任何可以理解的代码.此外,我想如果有人会检查我是否对我的代码做了任何疯狂.

非常感谢!

代码:

def generate_histogram_from_array_of_labels(Y=[], labels=[], xLabel="Class/Label", yLabel="Count", title="Histogram of Trainset"):
    plt.figure()
    plt.clf()

    colors = ["b", "r", "m", "w", "k", "g", "c", "y"]

    information = []
    for index in xrange(0, len(Y)):
        y = Y[index]

        if index > len(colors):
            color = colors[0]
        else:
            color = colors[index]

        if labels is None:
            label = "?"
        else:
            if index < len(labels):
                label = labels[index]
            else:
                label = "?"

        unique, counts = np.unique(y, …
Run Code Online (Sandbox Code Playgroud)

python matplotlib histogram matplotlib-basemap

4
推荐指数
1
解决办法
4711
查看次数

Docker 和 Spring boot 给出的“sun.awt.FontConfiguration.head”为 null

我目前有一个使用 Spring Boot 构建的应用程序。我正在使用 Jasper 报告。当我尝试生成报告时,出现以下错误:

java.lang.NullPointerException:无法从短数组加载,因为 java.desktop/sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1260) 处的“sun.awt.FontConfiguration.head”为空 ~[na:na]

该应用程序在我的计算机上运行良好,仅在 docker 中失败。

这是我的 Dockerfile:

FROM openjdk:17-alpine
EXPOSE 10093
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} site_informativo.jar

RUN apk update;
RUN apk add -f apt-utils;
RUN apk add -f libfreetype6;
RUN apk add --no-cache -f fontconfig;
RUN apk add -f fonts-dejavu;
RUN apk add -f msttcorefonts;
RUN apk add -f libfontconfig1;
RUN apk add -f freetype;
RUN fc-cache --force

ENTRYPOINT ["java","-jar","/site_informativo.jar"]
Run Code Online (Sandbox Code Playgroud)

我还在 pom.xml 中添加了 jasperreports-fonts EXTENSION 但问题仍然存在

<!-- https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports -->
<dependency> …
Run Code Online (Sandbox Code Playgroud)

java jasper-reports docker alpine-linux

3
推荐指数
1
解决办法
1万
查看次数