dotnet / dotnet/machinelearning-samples

Score of NaN in Product Recommendation

Open
#620 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
PowerShell
Stars
4.7k
Forks
2.7k
Avg merge
2d 22h
Merged PRs (30d)
1

Description

Hi,

I am trying to adapt the F# product recommendation sample to a small set of data that I have.

There are three main differences between my data and the Amazon data set:

1. My data set is much smaller, only about 78K entries.
2. I'm using UserId and ProductId, instead of ProductId and CoPurchasedProductId.
3. My ProductId is a string, rather than something that could be cast as a UInt32.

My issue is that I can only get a score of NaN for any prediction. I am unable to determine a way to diagnose this, maybe someone here can help?

Here's my code:

```fsharp
open Microsoft.ML
open Microsoft.ML.Data
open System
open Microsoft.ML.Trainers

[]
type ProductEntry =
{
[]
UserId : uint32
[]
ProductId : string
[]
Label : float32
}

[]
type Prediction = {Score : float32}

let trainDataPath = "/Users/nat/Downloads/user_product_prediction.csv"

let mlContext = MLContext()

let options = MatrixFactorizationTrainer.Options(MatrixColumnIndexColumnName = "UserIdEncoded",
MatrixRowIndexColumnName = "ProductIdEncoded",
LossFunction = MatrixFactorizationTrainer.LossFunctionType.SquareLossOneClass,
LabelColumnName = "Label",
Alpha = 0.01,
Lambda = 0.025)

let matrixFactorizationTrainer = mlContext.Recommendation().Trainers.MatrixFactorization(options)

let pipeline =
EstimatorChain().Append(
mlContext.Transforms.Conversion
.MapValueToKey(inputColumnName="UserId",outputColumnName="UserIdEncoded"))
.Append(
mlContext.Transforms.Conversion
.MapValueToKey(inputColumnName="ProductId",outputColumnName="ProductIdEncoded"))
.Append(matrixFactorizationTrainer)

let traindata =
let columns =
[|
TextLoader.Column("Label", DataKind.Single, 0)
TextLoader.Column("UserId", DataKind.UInt32, source = [|TextLoader.Range(0)|], keyCount = KeyCount 6248UL)
TextLoader.Column("ProductId", DataKind.String, source = [|TextLoader.Range(1)|])
|]
mlContext.Data.LoadFromTextFile(trainDataPath, columns, hasHeader=false, separatorChar=',')

let model = pipeline.Fit(traindata)

let predictionengine = mlContext.Model.CreatePredictionEngine(model)

let productEntry = {ProductId = "farfetch-13164877"; UserId = (uint32 10650); Label = 0.f}

let prediction = predictionengine.Predict productEntry

printfn ""
printfn "For product entry %A the predicted score is %f" productEntry prediction.Score
printf "=============== End of process, hit any key to finish ==============="
Console.ReadKey() |> ignore
```

Even when selecting a UserId-ProductId combination that is in the file, I get a score of NaN.

So, I have a few questions:

1. Am I mapping the ProductId string to a key correctly?
2. What are some possible causes of the NaN response?
3. I notice in the sample that the records of the fields have LoadColumn attributes, but they're also explicitly defined. Why is that?

My data set can be found here:
[user_product_prediction.txt](https://github.com/dotnet/machinelearning-samples/files/3542244/user_product_prediction.txt)

To try it out, just replace the Program.fs in the matrix factorization product recommendation [F# sample](https://github.com/dotnet/machinelearning-samples/tree/master/samples/fsharp/getting-started/MatrixFactorization_ProductRecommendation) with the program I pasted above. Download the txt file and change the path to the file as appropriate.

Any help or a pointer in the right direction would be deeply appreciated!

Thanks!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.