SQL Injection Detection
-
Please help make this code work. I'm trying to run it in Visual Studio 2022. Thanks.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Numpy;
using Python.Runtime;
using Keras;
using Keras.Layers;
using Keras.Models;
using Keras.Optimizers;
using Keras.losses;namespace SQLInjectionDetection
{
class Program
{
static void Main(string[] args)
{
// Load CSV file
var trainData = File.ReadAllLines("tokens.csv")
.Select(l => l.Split(','))
.Select(s => new { Token = s[0], Label = int.Parse(s[1]) })
.ToList();// Shuffle trainData var random = new Random(); trainData = trainData.OrderBy(d => random.Next()).ToList(); // Split trainData into training and validation sets var splitIndex = (int)(trainData.Count \* 0.8); var trainDataSubset = trainData.Take(splitIndex).ToList(); var testDataSubset = trainData.Skip(splitIndex).ToList(); // Define vocabulary and tokenize trainData var vocabulary = new HashSet(trainDataSubset.SelectMany(d => d.Token).Distinct()); var tokenToIndex = vocabulary.Select((c, i) => new { Token = c, Index = i }).ToDictionary(t => t.Token, t => t.Index); var maxSequenceLength = trainDataSubset.Max(d => d.Token.Length); var trainTokenized = Tokenize(trainDataSubset, tokenToIndex, maxSequenceLength); var testTokenized = Tokenize(testDataSubset, tokenToIndex, maxSequenceLength); // Build RNN model using (Py.GIL()) { dynamic keras = Py.Import("keras"); dynamic np = Py.Import("numpy"); var input = new Input(shape: 1000); var embedding = new Embedding(vocabulary.Count, 32).Apply(input); var lstm = new LSTM(32).Apply(embedding); var output = new Dense(1, activation: keras.activations.sigmoid).Apply(lstm); var model = new Model(inputs: input, outputs: output); model.Compile(optimizer: new Adam(), loss: new BinaryCrossentropy(), metrics: new\[\] { "accuracy" }); // Train model var trainX = trainTokenized.Item1; var trainY = trainTokenized.Item2; var testX = testTokenized.Item1;
-
Please help make this code work. I'm trying to run it in Visual Studio 2022. Thanks.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Numpy;
using Python.Runtime;
using Keras;
using Keras.Layers;
using Keras.Models;
using Keras.Optimizers;
using Keras.losses;namespace SQLInjectionDetection
{
class Program
{
static void Main(string[] args)
{
// Load CSV file
var trainData = File.ReadAllLines("tokens.csv")
.Select(l => l.Split(','))
.Select(s => new { Token = s[0], Label = int.Parse(s[1]) })
.ToList();// Shuffle trainData var random = new Random(); trainData = trainData.OrderBy(d => random.Next()).ToList(); // Split trainData into training and validation sets var splitIndex = (int)(trainData.Count \* 0.8); var trainDataSubset = trainData.Take(splitIndex).ToList(); var testDataSubset = trainData.Skip(splitIndex).ToList(); // Define vocabulary and tokenize trainData var vocabulary = new HashSet(trainDataSubset.SelectMany(d => d.Token).Distinct()); var tokenToIndex = vocabulary.Select((c, i) => new { Token = c, Index = i }).ToDictionary(t => t.Token, t => t.Index); var maxSequenceLength = trainDataSubset.Max(d => d.Token.Length); var trainTokenized = Tokenize(trainDataSubset, tokenToIndex, maxSequenceLength); var testTokenized = Tokenize(testDataSubset, tokenToIndex, maxSequenceLength); // Build RNN model using (Py.GIL()) { dynamic keras = Py.Import("keras"); dynamic np = Py.Import("numpy"); var input = new Input(shape: 1000); var embedding = new Embedding(vocabulary.Count, 32).Apply(input); var lstm = new LSTM(32).Apply(embedding); var output = new Dense(1, activation: keras.activations.sigmoid).Apply(lstm); var model = new Model(inputs: input, outputs: output); model.Compile(optimizer: new Adam(), loss: new BinaryCrossentropy(), metrics: new\[\] { "accuracy" }); // Train model var trainX = trainTokenized.Item1; var trainY = trainTokenized.Item2; var testX = testTokenized.Item1;
We have no idea what it is fully supposed to do, or what it does that you didn't expect / doesn't do that you did. And we have no access to your data so we couldn't test it if we wanted to. So, it's going to be up to you. Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need. Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why. Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!