Exemplo n.º 1
0
def test_multiclass(frontend, backend):
    clf, predictor = models[frontend]()

    # Create a conifer config
    cfg = conifer.backends.xilinxhls.auto_config()
    # Set the output directory to something unique
    cfg['OutputDir'] = 'prj_{}'.format(int(
        datetime.datetime.now().timestamp()))
    cfg['Precision'] = 'ap_fixed<32,16,AP_RND,AP_SAT>'
    cfg['XilinxPart'] = 'xcu250-figd2104-2L-e'
    cnf = conifer.model(clf, frontends[frontend], backends[backend], cfg)
    cnf.compile()

    y_hls, y_skl = predicts[frontend](predictor, X, y, cnf)
    np.testing.assert_allclose(y_hls, y_skl, rtol=1e-2, atol=1e-2)
    cnf.build()
Exemplo n.º 2
0
def vhdl_convert(train_skl):
    import conifer
    import datetime

    clf, X, y = train_skl

    # Create a conifer config
    cfg = conifer.backends.vivadohls.auto_config()
    cfg['Precision'] = 'ap_fixed<32,16>'
    # Set the output directory to something unique
    cfg['OutputDir'] = 'prj_{}'.format(int(
        datetime.datetime.now().timestamp()))
    cfg['XilinxPart'] = 'xcu250-figd2104-2L-e'

    # Create and compile the model
    model = conifer.model(clf, conifer.converters.sklearn,
                          conifer.backends.vhdl, cfg)
    model.compile()
    return model
Exemplo n.º 3
0
offset = int(X.shape[0] * 0.9)
X_train, y_train = X[:offset], y[:offset]
X_test, y_test = X[offset:], y[offset:]

# Train a BDT
clf = GradientBoostingRegressor(n_estimators=100,
                                max_depth=4,
                                min_samples_split=2,
                                learning_rate=0.01,
                                loss='ls')
clf.fit(X_train, y_train)

# Create a conifer config
cfg = conifer.backends.vivadohls.auto_config()
# Set the output directory to something unique
cfg['OutputDir'] = 'prj_{}'.format(int(datetime.datetime.now().timestamp()))
cfg['Precision'] = 'ap_fixed<64,32>'

# Create and compile the model
model = conifer.model(clf, conifer.converters.sklearn, conifer.backends.vhdl,
                      cfg)
model.compile()

# Run HLS C Simulation and get the output
#y_hls = model.decision_function(X)
y_hls, y_trees = model.decision_function(X)
y_skl = clf.predict(X)

# Synthesize the model
#model.build()
Exemplo n.º 4
0
    'max_depth': 3,
    'eta': 1,
    'alpha': 10,
    'gamma': 10,
    'objective': 'binary:logistic'
}
# Gamma controls pruning in XGBoost, Alpha controls L1 regularization

num_round = 20  # num_round is equivalent to number of trees
bst = xgb.train(param, dtrain, num_round)

# Create a conifer config
cfg = conifer.backends.vivadohls.auto_config()
# Set the output directory to something unique
cfg['OutputDir'] = 'prj_{}'.format(int(datetime.datetime.now().timestamp()))

# Create and compile the model
model = conifer.model(bst, conifer.converters.xgboost,
                      conifer.backends.vivadohls, cfg)
model.compile()

# Run HLS C Simulation and get the output
# xgboost 'predict' returns a probability like sklearn 'predict_proba'
# so we need to compute the probability from the decision_function returned
# by the HLS C Simulation
y_hls = expit(model.decision_function(X_test))
y_xgb = bst.predict(dtest)

# Synthesize the model
model.build()
Exemplo n.º 5
0
import conifer
import datetime

# Make a random dataset from sklearn 'hastie'
X, y = make_hastie_10_2(random_state=0)
X_train, X_test = X[:2000], X[2000:]
y_train, y_test = y[:2000], y[2000:]

# Train a BDT
clf = GradientBoostingClassifier(n_estimators=20,
                                 learning_rate=1.0,
                                 max_depth=3,
                                 random_state=0).fit(X_train, y_train)

# Create a conifer config
cfg = conifer.backends.xilinxhls.auto_config()
# Set the output directory to something unique
cfg['OutputDir'] = 'prj_{}'.format(int(datetime.datetime.now().timestamp()))

# Create and compile the model
model = conifer.model(clf, conifer.converters.sklearn,
                      conifer.backends.xilinxhls, cfg)
model.compile()

# Run HLS C Simulation and get the output
y_hls = model.decision_function(X)
y_skl = clf.decision_function(X)

# Synthesize the model
model.build()
Exemplo n.º 6
0
# Load the iris dataset from sklearn'
iris = load_iris()
X, y = iris.data, iris.target

# Train a BDT using the scikit-learn API
bst = xgb.XGBClassifier(n_estimators=20, max_depth=3,
                        learning_rate=1., objective='multi:softmax')
bst = bst.fit(X, y)

# Create a conifer config
cfg = conifer.backends.xilinxhls.auto_config()
# Set the output directory to something unique
cfg['OutputDir'] = 'prj_{}'.format(int(datetime.datetime.now().timestamp()))

# Create and compile the model
# We need to pass the Booster object to conifer, so from xgboost's scikit-learn API,
# we call bst.get_booster()
model = conifer.model(
    bst.get_booster(), conifer.converters.xgboost, conifer.backends.xilinxhls, cfg)
model.compile()

# Run HLS C Simulation and get the output
# xgboost 'predict' returns a probability like sklearn 'predict_proba'
# so we need to compute the probability from the decision_function returned
# by the HLS C Simulation
y_hls = softmax(model.decision_function(X), axis=1)
y_xgb = bst.predict_proba(X)

# Synthesize the model
model.build()
Exemplo n.º 7
0
from sklearn.datasets import make_hastie_10_2
from sklearn.ensemble import RandomForestClassifier
import conifer
import datetime

# Make a random dataset from sklearn 'hastie'
X, y = make_hastie_10_2(random_state=0)
X_train, X_test = X[:2000], X[2000:]
y_train, y_test = y[:2000], y[2000:]

# Train a BDT
clf = RandomForestClassifier(n_estimators=20, max_depth=3,
                             random_state=0).fit(X_train, y_train)

# Create a conifer config
cfg = conifer.backends.vivadohls.auto_config()
# Set the output directory to something unique
cfg['OutputDir'] = 'prj_{}'.format(int(datetime.datetime.now().timestamp()))

# Create and compile the model
model = conifer.model(clf, conifer.converters.sklearn,
                      conifer.backends.vivadohls, cfg)
model.compile()

# Run HLS C Simulation and get the output
y_hls = model.decision_function(X)
y_skl = clf.predict_proba(X)

# Synthesize the model
model.build()
#gApplication.Run()

# ------------------------------------------------------
#import imp

#foo = imp.load_source('conifer', '/afs/cern.ch/work/a/addropul/hls4ml/conifer/conifer/__init__.py')
#foo.MyClass()

# Create a conifer config
cfg = conifer.backends.vivadohls.auto_config()
# Set the output directory to something unique
cfg['OutputDir'] = 'prj_{}'.format(int(0504215))

# Create and compile the model
model = conifer.model(
    ET.parse('./dataset/weights/TMVAClassification_BDT.weights.xml'),
    conifer.converters.tmva, conifer.backends.vivadohls, cfg)
model.compile()

#need to get input variables into the proper form, numpy array
X = np.load(
    '/afs/cern.ch/user/a/addropul/CMSSW_10_6_0_pre4/src/L1Trigger/Run3Ntuplizer/test/June_July_2019/x_vbf072020_zb072020.npy'
)
print(np.shape(X))
# Run HLS C Simulation and get the output
y_hls = model.decision_function(X)
#y_skl = clf.decision_function(X)

# Synthesize the model
model.build()